How to call Sharepoint 2013 API by javascript in full trust solutions?
<p>I am developing a Farm Solution (full trust) with some custom WebParts. For one WebPart i want some ajax behaviour, so i thought: Why not use the JSOM API?
My web part just want to retrieve the new Announcements, so i added a new WebPart (Farm Solution), some HTML and the following javascript</p>

<pre><code>&lt;script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"&gt;&lt;/script&gt;

&lt;script type="text/javascript"&gt;

function retrieveListItems() {
  SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var list = web.get_lists().getByTitle("Announcements");
    ctx.load(list);
    ctx.executeQueryAsync(
          function () {
              console.log(list.get_title());
          },
          function (sender, args) {
              console.log(args.get_message());
          }
        );
  });
}
</code></pre>

<p></p>

<p>in my html i have a link to call the js function:</p>

<pre><code>&lt;a onclick="retrieveListItems(); return false;"&gt;Call&lt;/a&gt;
</code></pre>

<p>When i call the function i receive an 403 FORBIDDEN error, the google chrome console shows me the following</p>

<pre><code>/_vti_bin/client.svc/ProcessQuery 403 (FORBIDDEN)
     executeRequest         @ MicrosoftAjax.js:5
     executeRequest         @ MicrosoftAjax.js:5
     invoke                 @ MicrosoftAjax.js:5
     $31_0                  @ sp.runtime.js:2
     executeClientRequestAsync  @ sp.runtime.js:2
     executeQueryAsync      @ sp.runtime.js:2
     retrieveListItems      @ default.aspx:1091
     onclick                @ default.aspx:1118
</code></pre>

<p>And in the NETWORK tab of the Google Chrome console, in the PREVIEW (response) i see the following</p>

<pre><code>[{SchemaVersion: "15.0.0.0", LibraryVersion: "15.0.4420.1017", ErrorInfo: {,…},…}]
  0:{SchemaVersion: "15.0.0.0", LibraryVersion: "15.0.4420.1017", ErrorInfo: {,…},…}
  ErrorInfo:{,…}
  ErrorCode:-2130575251
  ErrorMessage:"A validação de segurança para esta página não é válida e pode estar corrompida. Use o botão Voltar do navegador da Web para tentar realizar a operação novamente."
  TRANSLATING: The security validation for this page is invalid and might be corrupted.  Please use your web browser's Back button to try your operation again.
  ErrorTypeName:"Microsoft.SharePoint.SPException"
  ErrorValue:null
  TraceCorrelationId:"78edb09d-fb1d-7012-0000-026d47d0154a"
  LibraryVersion:"15.0.4420.1017"
  SchemaVersion:"15.0.0.0"
  TraceCorrelationId:"78edb09d-fb1d-7012-0000-026d47d0154a"
</code></pre>

<p>I do some research i found something related with FormDigest, so i searched my HTML markup for the __REQUESTDIGEST and copied its value, called the function and realized that the digest sent is different. It could be the source of the problem?</p>

<p>I do more research and found this js function that cheats the RequestDigest:</p>

<pre><code> function CustomUpdateFormDigest() {
    if (window._spPageContextInfo != null) {
        var $v_2 = window._spPageContextInfo;
        var $v_3 = $v_2.webServerRelativeUrl;
        var $v_4 = window._spFormDigestRefreshInterval;
        UpdateFormDigest($v_3, $v_4);
    }
}
</code></pre>

<p>Called it before call my retrieveListItems() function but it not worked. Anyone can point me to the right direction? it is really a digest problem?</p>

<p>EDIT: I realised that if my page has my web part i get the error when i click in the link AND if i execute the script in the google chrome console. If i remove my webpart from page, i can execute the script in Google Chrome Console succesfully. </p>