Javascript AJAX POST JSON值
我有一篇AJAX文章,结果是JSON:Javascript AJAX POST JSON值,javascript,jquery,Javascript,Jquery,我有一篇AJAX文章,结果是JSON: $.ajax({ type: "POST", url: "../../api/test", data: JSON.stringify(source), contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { var upload = JSON.stringify(result); co
$.ajax({
type: "POST",
url: "../../api/test",
data: JSON.stringify(source),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var upload = JSON.stringify(result);
console.log(upload);
}
});
上传结果为:
{"Link":0,"Title":"d","Description":"dada","Keywords":"dad"}
如何获取Title的值?不要将结果字符串化,只需使用
result.Title
因为您已经有了JSON字符串,所以非常简单!
您所需要做的就是从分配给结果的变量中调用所需的属性
例如:
var post_response;
$.ajax({
type: "POST",
url: "../../api/test",
data: JSON.stringify(source),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
post_response = JSON.stringify(result);
console.log("Title: "+post_response.Title);
}
});
希望这有帮助。
结果是一个对象。我强烈建议只发布帖子,而不是创建一个服务器必须解码的JSON字符串。您使用的服务器技术是什么?PHP、ASP、Perl?是的。作为将来的参考,如果您对返回的内容有任何疑问,最好在浏览器中调试脚本。只要在success
函数中设置一个断点,您就可以看到返回的result
的内容。post\u response
在您尝试记录时将为空,因为它在异步回调中填充。jQuery知道您使用contentType json并将json转换为对象,你不需要JSON.parse(这里是你的输入错误)来处理^moonwave99,我的错。。我刚修改了我的代码@kitty,我刚刚复制了用户上面写的代码^