Stephan
Stephan

Reputation: 1868

200/'parsererror' with jquery on ajax post to a https

I've read loads of other questions about this argument, but none could solve my problem. I make a call to a php page in this way.

$.ajax({
        url: 'https://mydomain/page.php',
        type: "POST",
        data: { 
            "arg1": arg1,
            "arg2": arg2
        },
        success: function(data, textStatus, xhr) {
            //do stuff
        },
        error: function(xhr, textStatus) {
            alert("doLogin\n- readyState: "+xhr.readyState+"\n- status: "+xhr.status);
        }
    });

Now, if I put this stuff on the same server as the php it works fine. Troubles start when I launch it from localhost. In that case I receive the following in the xhr: readyState=0, status=0, statusText="error". Reading some answers on the topic it seems to be because of a same-origin restriction, so I added a few parameters to the call. notably:

   dataType:"jsonp",
   crossDomain: true,

Apparently this works better, cause now I receive readyState=4, status=200, statusText="success". Trouble is, textStatus="parsererror". I also tried other things as jsonpCallback, cache, async, jsonp in many configurations with no luck.

Now, I receive no data back, cause this call will only give me a cookie that I need.

My question is: am I doing things correctly, for starters? In both cases, what is the reason of such an error? Does the fact that I call a 'https'/POST change something, rather than a plain http/GET?

Second question is, later on I will have to call some webservices through soap requests, which will return data in xml. Will using this same technique work (assuming jQuery doc is fine and I can write dataType:"jsonp xml" to have it converted on the fly (and assuming it is the right technique as well))? I assume it won't be, as jsonp expects something on the line of callbackFN({...}) rather than an xml, right?

If none of this is correct, what would the correct way to proceed be? I can't touch the server, thus I am limited to client side.

Upvotes: 1

Views: 7511

Answers (2)

Okan Kocyigit
Okan Kocyigit

Reputation: 13421

If you set dataType as JSONP, you can only get data as JSON.

So if the url (https://mydomain/page.php) doesn't response a JSON object, you will get parsing error, because it tries to parse it and fails.

Upvotes: 2

devnull69
devnull69

Reputation: 16544

JSONP is for JSON format data only! So if you receive a parseerror this means that the output of your PHP might not be well-formed JSON

And no, it is not easily possible to have XML as response to a JSONP call ..

Upvotes: 0

Related Questions