3

I am trying to make a cross domain request. I am using jquery.ajax-cross-origin.min.js. The request looks like this

url: QueryURL,
      crossOrigin: true,
      async: false,
      dataType: 'application/json',
      type: "GET",
      success: function (data) {
          alert(data);
            fn(data);
        }
    });

The actual data returned from the page is json. But I am getting the below error in console

Refused to execute script because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

The surprising factor for me is I am facing this error only at a particular time of day. The code works fine during evenings and around 1am the same code starts giving error. Is there any daily limit for each user to access google cross domain API?

Please advice what I am missing here

nd99
  • 65
  • 2
  • 10
  • 1
    It sounds like you've told jQuery to expect a JSONP formatted response (to avoid CORS issues), or have included a `callback=?` in the URL, and yet the endpoint is not returning JSONP data. – Rory McCrossan Sep 10 '17 at 18:17
  • Yes, **callback=jQuery111303921457809541382_1505065420840&_=1505065420841 is getting appended to the URL.**. And the API is returning a json not jsonp – nd99 Sep 10 '17 at 18:25
  • That's your problem then. Remove that. I would guess you'll then get a 'no access control error' which means that you cannot make the request you're trying from JS code. – Rory McCrossan Sep 10 '17 at 19:04
  • the callback part of url is getting appended automatically and hence i am not able to remove it. I have used **** to make cross domain request. Can you please tell me if there is any way to prevent the callback part from automatically appending – nd99 Sep 10 '17 at 19:12
  • 1
    Remove `crossDomain: true`. However there is no way of making a cross domain request if the recipient server does not return CORS headers. You cannot use JSONP as you've already stated that the response is JSON, and they are not interchangeable. The best method here would be to make the cross-domain request from the server, not from client-side JS. – Rory McCrossan Sep 10 '17 at 19:32
  • The server is an external API and so I do not have any control over it.. If i remove crossOrign: true i will get No 'Access-Control-Allow-Origin' error and that is why i went for ajax cross origin plugin. Can you please tell me if there is any way to take this problem forward – nd99 Sep 10 '17 at 19:40
  • If you're getting that error then it's not possible to call that API with JS as they have not enabled CORS. You'll need to send the request from the server instead. – Rory McCrossan Sep 10 '17 at 21:05

0 Answers0