0

I have to send details to the web service. I have made a JSON string in which the details are stored, str_table is the JSON string which I have to pass to the web service.

I tried many things from Google, but nothing worked. Following is among those things which I tried. Please check where I am going wrong.

$.ajax({
   type:"POST",
   url:"hostname/proj_folder/name.php?content="+str_table,
   contentType: "application/json; charset=utf-8",
   crossDomain:true,
   dataType:'json',
   success: alert("in success"),
   error: alert("in error")    
});

I have used data in the following code, and now it alerts error[object Object]

 $.ajax({
            type:"POST",
            url:"hostname/proj_folder/name.php?",
            contentType: "application/json; charset=utf-8",
        data:JSON.stringify('content='+str_table),
            crossDomain:true,
        dataType:'json',
        success: function(data){
                alert('succes' + data);
            },
        error: function(data){
                alert('error' + data);
        }
    });
Deepika
  • 331
  • 2
  • 7
  • 20
  • What exactly isn't working? I can already see that your `success` and `error` handlers are broken. They will simply alert two messages and then set the handlers to `undefined`. I'm also wondering why you aren't using the `data` option like documented in http://api.jquery.com/jQuery.ajax/#entry-examples – Tim Apr 22 '14 at 06:50
  • have you tried debugging using chrome console or firebug ? I think you are not being alerted because your `alert()` should be within anonymous `function(data) { }` handler after `success:` and `error:` labels – anu Apr 22 '14 at 06:52
  • @TheShellfishMeme: Yes exactly, it simply gives those 2 alerts. I have tried with data too, but doesn't work. I had referred the following link http://stackoverflow.com/questions/5046930/jquery-send-string-as-post-parameters I am sending 5 different things by storing them in JSON string. But at the server side nothing is inserted into tables. – Deepika Apr 22 '14 at 06:59
  • @Deepika What happens when you send the exact same request to the url directly using your browser ? Does the content get inserted then ? If not then your server side code does not work. – anu Apr 22 '14 at 07:08
  • Any access denied error in console (Origin xxxx is not allowed by Access-Control-Allow-Origin)? Are you sending the request to a cross domain host? As @Anupam mentioned, you need to assign a function to success and error label. – Chickenrice Apr 22 '14 at 07:08
  • @Anupam The content gets inserted on server side. The JSON string is created at my side and I have stored it in local storage. When the same string is run on browser on server side, the content gets inserted. Please find the edits. – Deepika Apr 22 '14 at 07:16
  • @Chickenrice no such error. – Deepika Apr 22 '14 at 07:23
  • @Deepika What does the HTTP response header contain ? ( See in console, or firebug) – anu Apr 22 '14 at 07:31
  • @Deepika Also, if `str_table` is already a JSON string you don't want to stringify it again, You are probably looking to do `JSON.parse()`. And you should also get rid of `?` at the end of URL. The idea of query string applies only to GET, not POST. – anu Apr 22 '14 at 07:37

0 Answers0