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

  • Question

  • 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.

    Tuesday, April 28, 2015 10:51 AM

Answers

  • 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.


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    Friday, July 24, 2015 3:50 AM

All replies

  • 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


    Dhana-Chennai-India

    Thursday, July 23, 2015 12:00 PM
  • 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.


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    Friday, July 24, 2015 3:50 AM
  • 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.


    Dhana-Chennai-India

    Friday, July 24, 2015 7:11 AM
  • Hi,

    Can you check the following link:

    http://sanderlesage.blogspot.com.au/2014/07/copy-file-cross-site-collection-csom-rest.html

    As shown in the link, the author has created two RequestExecutors (though RequestExecutor is used in app concept, but author has used it across site collection).


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    Friday, July 24, 2015 7:50 AM