How to transfer JSON data to soothing web services via ajax, and also how to get JSON data?

Hi, I am new to JSON. My question is how to pass JSON data to soothing web services via ajax?

Please help me.

I tried the following code, but I'm not sure about it

MY INDEX PAGE

<script type="text/javascript"> $(document).ready(function(){ var uname = document.getElementById("uname").value(); var password = document.getElementById("pwd").value(); $('#ok').click(function(){ $.ajax({ url:'http://localhost:8090/LoginAuthRWS/rest/orders', type:'post', dataType: 'Jsondemo', success: function(data) { $('#name').val(data.name); $('#email').val(data.email); var JSONObject= { "uname":uname, "password":password }; } }); }); }); </script> 
+7
json jquery rest ajax
source share
6 answers
 var JSONObject= {"uname":uname, "password":password }; var jsonData = JSON.parse( JSONObject ); var request = $.ajax({ url: "rest/orders", type: "POST", data: jsonData, dataType: "json" }); 
+9
source share

Problems with your code:

  • .value is a property, not a function
  • Do you want to pass json use data from $.ajax
  • No data type like Jsondemo , you have to use JSON
  • If the data response is not JSON, you can use $.parseJSON to convert to JSON

Full code

 $(document).ready(function(){ $('#ok').click(function(){ var uname = document.getElementById("uname").value; var password = document.getElementById("pwd").value; var JSONObject= { "uname":uname, "password":password }; $.ajax({ url:'http://localhost:8090/LoginAuthRWS/rest/orders', type:'post', data : JSONObject, dataType: 'JSON', success: function(data) { var jsonData = $.parseJSON(data); //if data is not json $('#name').val(jsonData.name); $('#email').val(jsonData.email); } }); }); }); 
+4
source share

You want to do something like this:

 $('#ok').click(function(){ $.ajax({ url:'http://localhost:8090/LoginAuthRWS/rest/orders', type:'post', dataType: 'json', data: { name: "John", location: "Boston" } success: function(data) { response = $.parseJSON(data); $('#name').val(response.name); $('#email').val(response.email); } }); }); 

A few notes:

  • dataType should almost always be xml or json . Sometimes jQuery can guess correctly if you don't provide anything. But it must be the real thing.
  • Since you are making a message, you need to send data to the REST endpoint. This is what I have in data . Note that the data type matches the value in dataType . Also note that you can use the $.post method to make a much simpler post using jQuery.
  • The data parameter for the success callback needs to be parsed as JSON first (assuming it returns), because it is of type PlainObject , as described. What $.parseJSON . Once you do this, you can move around the JSON tree as needed to do what you need. You may be able to leave without doing this.

Hope this helps.

+1
source share

jQuery dataType Link .

Possible dataType values: xml , json , script or html

Try the following:

 var dataToServer = { uname : document.getElementById("uname").value, document.getElementById("pwd").value }; $.ajax({ url:'http://localhost:8090/LoginAuthRWS/rest/orders', type:'post', // or put contentType: 'application/json', // type of data data: JSON.stringify(dataToServer) // make JSON string dataType: 'json', // type of return result success: function(data) { $('#name').val(data.name); $('#email').val(data.email); } }); 
+1
source share

To pass values ​​to web services, Ajax has a data strong> attribute .

 <script type="text/javascript"> $(document).ready(function(){ var uname = document.getElementById("uname").value; var password = document.getElementById("pwd").value; $('#ok').click(function(){ $.ajax({ url:'http://localhost:8090/LoginAuthRWS/rest/orders', type:'post', dataType: 'Json', data:{ uname:uname, password:password }, success: function(data) { $('#name').val(data.name); $('#email').val(data.email); } }); }); }); </script> 
+1
source share

You can pass json data as the request body as follows:

  var JSONObject= {"uname":uname, "password":password }; $.ajax({ url : env + 'rest/orders', type : 'POST', headers: { 'Content-Type':'application/json' }, data : JSON.stringify(JSONObject), dataType : "json", }); 
0
source share

All Articles