0

Here is my script to make a request cross-domain request. Since the data type is JSONP I would expect to receive a JSONP response, however the other server will only return a JSON file (which I can view using chrome developer console) but I don't know how to extract that JSON file.

For the code, it will run to "error" and "commplete" sections but not success, the error will be reported as the "callback function <....name of callback function....> was not called. I do not know how to work about it, please help!

$.ajax({
    type: "GET",
    crossDomain : true,
    url:"http://192.168.100.2:3000/api/dashboards/db/practice1/",   
    dataType: "jsonp",
    Authorization: "Bearer eyJrIjoieWsyRW5URG4xZ1ZjVHhCd0Q0UmNjZmx1ODZoMmRiVEwiLCJuIjoidmlld2VyIiwiaWQiOjF9",
    success: function(data){
        alert("success");
        console.log(parseJSON(data));
    },
    error: function(data, httpReq, status, exception) {
        alert(httpReq + " " + status + " " + exception);
        //console.log(data.success);
    },
    complete : function(data,httpReq,status,exception){
        //apiStatus(data);
        //console.log(data.success);
    }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
thien nguyen
  • 306
  • 5
  • 10
  • put in also the json you receive from the server – Velimir Tchatchevsky Jun 06 '16 at 07:08
  • `Authorization` is not a property of `$.ajax` - it's a header. You need to place it in an object you provide to the `header` property. – Rory McCrossan Jun 06 '16 at 07:09
  • More importantly, if the response from the server is JSON, simply requesting JSONP will change nothing. The request will not change the format of the response in any way. If you require a JSONP response, you will need to change the server code, assuming you have access. Also note that a JSON request will not work cross-domain unless you have enabled CORS on the server. – Rory McCrossan Jun 06 '16 at 07:12
  • Authorization works fine! its just that server supposed to return "callbackfunct({"meta":{"type":.....}})" but return only json instead – thien nguyen Jun 06 '16 at 07:12
  • try requesting json, they might of enabled cors server side – Velimir Tchatchevsky Jun 06 '16 at 07:14
  • @RoryMcCrossan: yes, you are correct, that is what I found from different posts, but I do not have access to the server hence cannot enable CORS, and using JSONP is the only way to get request for it. Hence I dont have any other option. Could you think of another way? – thien nguyen Jun 06 '16 at 07:14
  • @VelimirTchatchevsky Thanks for the suggestion, I tried to request for json but I got 'Access-Control-Allow-Origin' error again! – thien nguyen Jun 06 '16 at 07:15
  • @thiennguyen in that case, no. You cannot make the request to that server through JS code alone due to the [Same Origin Policy](http://en.wikipedia.org/wiki/Same-origin_policy). One workaround would be to use a server-side proxy on your local domain. See the question I marked as a duplicate for more information on the problem and several other workarounds. – Rory McCrossan Jun 06 '16 at 07:16
  • @RoryMcCrossan Thanks so much for the suggestion. That might be the only way! – thien nguyen Jun 06 '16 at 07:18

0 Answers0