0

So I have to send some data to a php page, and it will return me another php page based on my data.

I send the data this way:

$(document).ready(function() {
    $.ajax({
        url: '//www.example.com/page.php',
        type: "post",
        dataType: 'jsonp',
        data: { myvar:myvalue }, 
        success: function(response) { console.log("success."); },
        error: function(XMLHttpRequest, textStatus, errorThrown) { console.log("error."); },
        complete: function() { console.log("complete."); }
    });
});
  • It shows an alert saying jQuery180014405992737595236_1357861668479 was not called (numbers are copied from other question) I think the reason is that it's expecting a json result from the page, when it's not.
  • In Chrome it says Uncaught SyntaxError: Unexpected token < referring to the returned php page, so I assume that my code isnt expecting that kind of file to be returned.

To sum up, this works, but that jQuery alert and the console error needs to be fixed, and I think the right way would be handling properly the returned result page. I hope you guys can help me fix it that seems quite a simple task, but Im really new to this. Thanks

Removing the dataType: 'jsonp' or changing it to 'json' turns out on my script not being executed and getting the following error:

  • XMLHttpRequest cannot load http://www.example.com/page.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://myserver.com/myPage' is therefore not allowed access.
Dane411
  • 823
  • 1
  • 13
  • 27
  • is the php script on a different domain than the where the javascript is executing (in the example, does the javascript run on a page of www.example.com) ? – RichardBernards Nov 19 '14 at 15:15
  • Try taking off your `dataType` parameter, the server is sending back HTML. You may have to decode the JSON from a string if the server does not send consistent responses. – John Ruddell Nov 19 '14 at 15:16
  • And the dataType `jsonp` does require json to be returned, if it is HTML you are expecting, change it to `html` – RichardBernards Nov 19 '14 at 15:16
  • does that php page know it's being invoked via jsonp? You can't just fire json requests off at any random server - the server has to know **HOW** respond properly. given you get that "not called" error, then I'm guessing the server is NOT jsonp aware and isn't outputting with the proper JS callback function wrapper. – Marc B Nov 19 '14 at 15:18
  • I thought I needed to specify the data I'm sending is json but it was giving me some errors that got fixed changing it to jsonp, should I leave it as json, or just remove that line? – Dane411 Nov 19 '14 at 15:31
  • Dane see my answer, when you say jsonp it is expecting a jsonp response to be sent back. so if you want a html page to be sent back then you need to remove that line or change it to `dataType: 'html'` but if you have an issue with sending the data then json encode it and then decode it on the server – John Ruddell Nov 19 '14 at 15:43
  • @JohnRuddell Check my edit, if I change or remove the jsonp, it doesnt work anymore and I get worse errors. Previously it was working, but I was getting a jQuery error. – Dane411 Nov 19 '14 at 15:59
  • http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – John Ruddell Nov 19 '14 at 16:01
  • "As you can see from this example, CORS support requires coordination between both the server and client." - http://www.html5rocks.com/en/tutorials/cors/ If I get that right, in order to be able to use CORS, I should be able to modify both example.com and myServer.com, and example.com isn't mine.. – Dane411 Nov 21 '14 at 03:04

1 Answers1

0

I think the reason is that it's expecting a json result from the page

It's expecting a JSONP response. (JSONP is not JSON). You said:

dataType: 'jsonp',

… which explicitly forces jQuery to treat the response as JSONP (and, as a side effect, GET).

the returned php page, so I assume that my code isnt expecting that kind of file to be returned.

The server shouldn't be returning a PHP page. It should be executing the PHP code and returning whatever that outputs. It looks like it is outputting HTML.

You need to either:

  • Not tell your script to expect JSONP. (Note that you'll probably then have to configure CORS on the server to deal with same origin issues) or
  • Change the PHP to return JSONP
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • My script is sending the data to another server that I have no control over, so all I can change is the code I provided. It will return a html, but I dont know whats wrong in my code and how to get the returned page. – Dane411 Nov 19 '14 at 15:28
  • @Dane411 — I told you what is wrong with your code. You're telling jQuery to treat the response as JSONP. The response is not JSONP. If the server isn't going to grant you permission to instruct your visitors browser to fetch you data from it (via CORS) then you can't access it directly with Ajax. – Quentin Nov 19 '14 at 15:36