简体   繁体   English

jQuery ajax在发送帖子请求时不起作用

[英]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 我尝试使用ajxt将发布请求发送到我的宁静服务,这是我的ajax代码

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. 那是一个JavaScript对象,而不是JSON文本。

var jsonData = JSON.parse( JSONObject ); 

That will error (or return null ) because you aren't passing it a string containing a JSON text. 这将出错(或返回null ),因为您没有向其传递包含JSON文本的字符串。

Your later code is expecting a string containing a JSON text though. 您的后续代码期望使用的字符串包含JSON文本。

You want JSON.stringify not JSON.parse . 您想要JSON.stringify而不是JSON.parse

You need to change your "JSON.parse" to "JSON.stringify". 您需要将“ JSON.parse”更改为“ JSON.stringify”。 You called the opposite function that you should have. 您调用了应该具有的相反函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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