Trying to Access SharePoint 2013 Rest Api in HTML page but getting Mime Type Exception with Status Success

I am trying to invoke the SharePoint Rest Api using HTML page. I have included the Access Control Allow Origin to the web.config file. I am getting Readty State and Status Success but still I am getting the below error.

Refused to execute script from 'http://<server>/_api//web/lists?callback=jQuery172045857910416089_1430217181282&_=1430217363882' because its MIME type ('application/atom+xml') is not executable, and strict MIME type checking is enabled.

    <script>
        $(document).ready(function () {
            $("#KMPDiscussions").click(function () {
                //$.support.cors = true;
                $.ajax({
                    url: "http://<server>/_api//web/lists",
                    dataType: "jsonp",
                    type: "GET",
                    method: "GET",
                    contentType: "application/javascript",
                    headers: {
                        "content-type": "application/json;odata=verbose",
                        "accept": "application/json;odata=verbose",
                    },
                    success: function onSuccess(data) {
                        alert("Inside Alert");
                        }
                    },
                    error: function onError(data){
                        alert("Error: " + data);
                }
                });
            });
        });
    </script>

It always hits the error on callback.

Is there any other way that I can invoke SharePoint Rest Api from a Cross Domain. Please Help.

April 28th, 2015 10:51am

Hi Prinse,

Do you have any update on above issue. I am running with same issue on Chrome..

Let me know if you have any suggestion/fix.

Thanks,

Dhana

Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 8:06am

Hi,

A simple script like below should work (reference), considering you are trying from inside SharePoint.

$.ajax({
  url: url + "/_api/web/lists/",
  method: "GET",
  headers: { "Accept": "application/json; odata=verbose" },
  success: function (data) {
      // Returning the results
      complete(data);
  },
  error: function (data) {
      failure(data);
  }
});
However things might be complex if you are trying to execute the code from outside SharePoint as the security, cross domain issue can come up.

July 23rd, 2015 11:56pm

Hi Sohel,

Exactly the same code am using to fetch the list items. But issue comes when i call the different site collection list.

Though, It works fine in IE browsers and not in Chrome.

         

 Errors thrown only with chrome.

error 1: Failed to load resource : the server responded with a status of 401(unauthorized)

error 2: XML HTtpRequest Canot load 

         No 'Access-control-Allow-Origin' header is present on the requested resource. Origin 'url' is therefor not allowed access. The response had Http Status code 401

Thanks,

Dhana.

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2015 3:17am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics