2

I am trying to use JSONP to get around the same origin policy, but as far as I understood, I have the mentioned error because the server is not actually returning a JSONP response. But if I use json as my dataType then I get an "XMLHttpRequest cannot load, No 'Access-Control-Allow-Origin' header is present on the requested resource" Here is my code:

$.ajax({
      url : "https://maps.googleapis.com/maps/api/place/nearbysearch/json?    location=-33.86,151.195&radius=5000&type=ATM&keyword=ATM&key=MyAPIKey",
      type : "GET",
      dataType: 'jsonp',
      success:function(data){
         console.log(data);
      }
    })

A similar problem can be found here [json Uncaught SyntaxError: Unexpected token : ] but only served to understand what is going on rather than solving the problem. I am relatively new to this so please I will appreciate a step-by-step guide to solving this.

Having same problem on chrome and firefox.

Community
  • 1
  • 1
S. Rehan
  • 69
  • 2

1 Answers1

0

You seem to be missing the callback name, having an extra "type" parameter, and not using the 'data' parameter to pass the request params:

https://learn.jquery.com/ajax/working-with-jsonp/

EDIT: I've taken a closer look at your code and tried it in plunker. I've tried this:

$(function(){
$.ajax({
      url : "https://maps.googleapis.com/maps/api/place/nearbysearch/json",
      data:{
        location:"-33.86,151.195",
        radius:"5000",
        type:"ATM",
        keyword:"ATM",
        key:"MyAPIKey"

      },
      dataType: 'jsonp',
      success:function(data){
         console.log(data);
      },
      error:function(data){
         console.log("err",data);
      }
    })
});

What I got back was an error that your API key is not correct (ofc, its not). But if you will put the proper key in, then JSONP call will still not work because API that you are calling is not returning you JSONP, but JSON. (as you can see at the end of the URL).

Server side should specifically support JSONP in order to use it, cause it is supposed to wrap the response in a script that issues a callback call with data.

So:

  • ajax JSONP call works

  • given server resource does not return JSONP

The error you are seeing is the result of your application trying to execute resulted JSON as a script.

EDIT2:

In general, "Access-Control-Allow-Origin" header is set by the server in the response. If your referrer page origin does not match the setting on that header, the browser will do what it does now. You cannot control it in the request.

See for reference on CORS

Its all server-side/under the hood in browser. But there were a similar question in here:

(Another question on same topic)Access-Control-Allow-Origin using Google Maps JavaScript API v3.

Try to use googles library if you can. Perhaps it does some other call in before requesting the resource to enable teh CORS from your site.

Community
  • 1
  • 1
Vladimir M
  • 4,403
  • 1
  • 19
  • 24
  • I have tried this but still the same problem. Any suggestions? Thanks!.. here is my new code: $.ajax({ url: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.86,151.195&radius=5000&type=ATM&keyword=ATM&key=AIzaSyAmqJGpDDSMVZimLQ3yPeLKHqqd7VVmmNg", jsonp: "callback", dataType: "jsonp", data: { format: "json" }, success: function( response ) { console.log( response ); // server response } }); – S. Rehan Nov 24 '16 at 12:54
  • pls. see the edited part of the answer. your resource is JSON resource and not JSONP – Vladimir M Nov 24 '16 at 16:51
  • Thanks a lot. Your explaination makes a lot of sense. So to sum up, I just cannot use JSONP as the server does not support it. So I have to use JSON. But I get stuck into another problem when using JSON which is "XMLHttpRequest cannot load, No 'Access-Control-Allow-Origin' header is present on the requested resource" Do you have any idea how could I over come this problem when using JSON? Thanks! – S. Rehan Nov 24 '16 at 21:26
  • again more text then fits the comment :). see Edit2. – Vladimir M Nov 24 '16 at 22:03
  • Thanks once again, I will try that and get back with the result! – S. Rehan Nov 25 '16 at 14:59