简体   繁体   中英

jQuery ajax is not working when send post request

i try to use ajxt to send post request to my restful service here is my ajax code

var lat = marker.getPosition().lat();   
var lng = marker.getPosition().lng();   
//xmlhttp.open("GET","http://192.168.1.100:8080/MapDemo/service/add?name=hieugie333&longitude=123&latitude=321",true);
//xmlhttp.send();
//document.getElementById("message").innerHTML=xmlhttp.responseText;
var JSONObject= {"name":name, "longitude":lng,"latitude":lat };
var jsonData = JSON.parse( JSONObject );    

var request = $.ajax({
  url: "http://192.168.1.100:8080/MapDemo/service/add",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: jsonData,
  dataType: "json"
}); 

can anyone help me in this case ?

var JSONObject= {"name":name, "longitude":lng,"latitude":lat };

That is a JavaScript object, not a JSON text.

var jsonData = JSON.parse( JSONObject ); 

That will error (or return null ) because you aren't passing it a string containing a JSON text.

Your later code is expecting a string containing a JSON text though.

You want JSON.stringify not JSON.parse .

You need to change your "JSON.parse" to "JSON.stringify". You called the opposite function that you should have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM