Deepika
Deepika

Reputation: 331

Failed to load resource error for ajax call in javascript

I know there are many such questions, i tried those answers but my problem is not solved.

Am sending string to the webservice. But, values are not inserted in tables on server, whereas if I run the webservice in browser, it inserts the values. So where is the problem exactly?

https://coderwall.com/p/5nccwq

Failed to load resource under Chrome

I referred the second link and tried to change according to the first answer in it, but doesn't work.

I tried to execute it in incognito mode, but no use.

Please help with your answers.

var targeturl="http://hostname/projfolder/webservice.php?orderList="+str_table;
        console.log(targeturl);
$.ajax({
        type:"POST",
        url:targeturl,
        contentType: "application/json; charset=utf-8",
        crossDomain:true,
        dataType:'jsonp',
        success:function (data)
           {
            alert("in success");
            var parsedata=JSON.parse(JSON.stringify(data));
            alert("parsedata: "+parsedata);
            var stats=parsedata["Status"];
            alert("logindata: "+stats);

            if("1"==stats)
            {   
                alert("success");
            }
            else
            {
                alert("failed");
            }
          }
});

Actually I had forgot adding http://, but I did it now, now here I get Unexpected token : error

I used many web services for the same project but did not go through such issue.

Upvotes: 0

Views: 5537

Answers (1)

closure
closure

Reputation: 7452

Your server should understand and respond in JSONP. It cannot simply return JSON.

Example:

  • suppose server needs to return json like {status:"1", msg:"success"}
  • the response from the server will be like callback({status:"1", msg:"success"})
  • The callback is random string that is automatically generated by JQuery and set in the query string of the request URL with parameter named callback.
  • Your server should read this parameter from query string and then format the response as described in 2nd point above.

Please read:

http://api.jquery.com/jquery.getjson/

http://en.wikipedia.org/wiki/JSONP

Upvotes: 1

Related Questions