0

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.

Community
  • 1
  • 1
Deepika
  • 331
  • 2
  • 7
  • 20
  • Can you post some code? – haxtbh Apr 29 '14 at 09:28
  • How are you sending string to webservice? – closure Apr 29 '14 at 09:31
  • please find the edits. – Deepika Apr 29 '14 at 09:41
  • I think data you are receiving is not well formed json. Can you please do console.log(data) as first line of the success function – closure Apr 29 '14 at 09:52
  • Its not entering the success function, none of the alerts is displayed. Data is getting inserted in tables now. Am not receiving anything, infact i am sending json by storing it in variable and in return i am getting status. But, still I have the unexpected token : error – Deepika Apr 29 '14 at 10:06
  • Remove dataType:'jsonp', – closure Apr 29 '14 at 10:08
  • After removing as per you said I get the following 2 errors: 1)Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 2)XMLHttpRequest cannot load http://hostname/projfolder/webservice.php?orderList={my data which goes to server}. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. – Deepika Apr 29 '14 at 11:00
  • My data gets inserted to the server, but I don't know why it does not enter the success parameter. – Deepika Apr 29 '14 at 11:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51676/discussion-between-closure-and-deepika) – closure Apr 29 '14 at 11:22

1 Answers1

1

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

closure
  • 7,412
  • 1
  • 23
  • 23