0

I am creating an app using HTML, jQuery Mobile, jQuery and Cordova. My requirement is to authenticate an user from the app by calling a remote server. I am developing my localhost WAMP stack.

My jQuery is as below.

$.ajax({
    type: 'POST',
    url: 'http://localhost/server/',
    crossDomain: true,
    data:{appkey:'1234567',action:'authenticate', username: u, password :p},
    dataType: 'jsonp',
    async: true,
    success: function (data){
        console.log(data);
     },
     error: function (jqXHR, textStatus, errorThrown) {
         alert("Invalid Username or Password");
         window.location = "index.html";
     }
});

The remote call url is below. JQuery automatially adds ?callback= to the remote url as the datatype is jsonp, which is fine.

localhost/server/?callback=jQuery18306688079617451876_1383495199431…4567&action=authenticate&username=fdf&password=fdfdf&_=1383495208981

The response from the remote url is as below in Chrome Developer console.

{"success":"false"}

In the PHP part at server, I am using the below code to return the json object.

header('Content-type: application/json');
echo json_encode($araryVal);

It looks for me that all things are perfect, but my code breakpoint never reaches the success section of the ajax call. It always break out in error section.

In the developer console I see the error message.

Uncaught SyntaxError: Unexpected token :

JsonLint says the json is valid. Please let me know what I am doing silly.

2 Answers 2

2

I don't think your data type is JSONP, it's just JSON. If it were JSONP, your content-type should be "application/javascript" and your response text should wrap the JSON object it returns with the javascript function provided to the server in the callback parameter.

(Actually your error is probably PHP emitting a notice about an undefined variable: araryVal ... You sure that shouldn't be arrayVal? :)

Sign up to request clarification or add additional context in comments.

4 Comments

Changing the MINE to application/javascript does not help.. still the same. The error is from javascript and not from PHP :)
I think Fabio is correct. {"success":"false"} is valid JSON, not valid JSON-P. You should be seeing jquery_1598713487698({"success":"false"}) or something similar. The P in JSONP is the jquery_1598713487698() padding. On the server side you need to re-emit the callback= get parameter as a wrapper around your JSON.
Yea, thanks Mike for explaining better. Another option is to not tell jquery that the datatype is "jsonp", but instead "json"
I accept this as the solution. I changed the PHP code to include the callback function name along with the json output. I will edit my question to post what I actually did.
1

I already accepted the answer provided by @Fabio. I just want to provide the technical details which solved my problem.

The hint from Fabio helped me to do some research on this. Thanks man.

I have added the callback options to the ajax call as below.

$.ajax({
    type: 'POST',
    url: 'http://localhost/server/',
    crossDomain: true,
    data:{appkey:'1234567',action:'authenticate', username: u, password :p},
    dataType: 'jsonp',
    contentType: "application/javascript",
    jsonp: 'callback',
    jsonpCallback: 'mycallback',
    async: true,
    success: function (data){
        console.log(data);
     },
     error: function (jqXHR, textStatus, errorThrown) {
         alert("Invalid Username or Password");
         window.location = "index.html";
     }
});

The remote call url is as below after the above change. JQuery now add "mycallback" instead of some random callback value "jQuery18306688079617451876_1383495199431…4567" for callback parameter.

localhost/server/?callback=mycallback&action=authenticate&username=fdf&password=fdfdf&_=1383495208981

In the PHP side I modified the output to include the callback value with the json string.

header('Content-type: application/javascript');
echo "mycallback(" . json_encode($araryVal) . ")";

Today was the day I understood how to use jsonp with callback. Thanks to all.

2 Comments

PS. The content-type seems to still be set to application/json . If you're providing it for the reference of others, you should make it the correct mime-type of application/javascript
Yes, JSONP is actually javascript not just JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.