0

I am running into problem requesting data from an API. The documentation states that I must make a post request. My goal is to make the request with jQuery, then style the resulting data.

Since I'm using a Cross-Domain-Request, I know that I have to use jsonp. The issue is that I get parseerror when I run this code because the server is returning an XML error. (The server defaults to an xml response if no format is specified)

$.ajax({
    type: 'POST',
    url: url,
    data: {format: 'json'},
    dataType: 'jsonp',
    success: function(data) {
      console.log("Success.");
    },
    error: function(data, error, err) {
      console.log(data.status);
      console.log(error);
      console.log(err);
    }
  });

Inside the inspector I see:

Uncaught SyntaxError: Unexpected token <           
menu?&callback=jQuery2240569993828256961_1469752734662&format=json&_=1469752734663:1 

This is the XML response...

<?xml version="1.0" encoding="UTF-8"?><response> <response_header> <response_code>ERROR</response_code>
</response_header>
 <response_details> <error_details>The post parameters contain an invalid or disabled API ID</error_details>
</response_details>
</response>

Because it returns XML, I can determine that the server is not even recognizing my parameter {format: 'json'}, because here is the same request in Python:

from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = url # Set destination URL here
post_fields = {'format':'json'}     # Set POST fields here

request = Request(url, urlencode(post_fields).encode())
json = urlopen(request).read().decode()
print(json)

And the resulting JSON response:

{"response_header":{"response_code":"ERROR"},"response_details":{"error_details":"The post parameters contain an invalid or disabled API ID"}}

As you can see, it responds with JSON demonstrating that the server read the post request.

For some reason, the AJAX request is not sending the parameters through which means I can't even get back the actual data I need by including the api key + api id.

Can anyone tell me what is going on here?

Thank you.

  • Why are you setting `dataType: 'jsonp',`? That's why you're getting this `menu?&callback=jQuery2240569993828256961_1469752734662&format=json&_=1469752734663:1` . You can't POST via jsonp. It isn't possible. And, your server must be configured to accept a JSONP request and send a JSONP response which lives in a script tag. JSONP is mostly obsolete now that servers can specify CORS to allow cross origin requests. – jfriend00 Jul 29 '16 at 01:16
  • So, is there any way to post cross-domain and get the data that the server responds? – Artimath Jul 29 '16 at 05:26
  • Yes, it's called CORS and the server has to support it for you to be able to use it from a browser Ajax call. You don't see the issue in Python because it is the browser that is restricting the cross origin request for good security reasons. Or, you can create your own server proxy. – jfriend00 Jul 29 '16 at 05:27
  • See [this answer](http://stackoverflow.com/questions/38558014/no-access-control-allow-origin-header-present-angularjs/38558109#38558109) for your various options. – jfriend00 Jul 29 '16 at 05:31
  • Thank you for your help. – Artimath Jul 29 '16 at 06:14

0 Answers0