何时在ajax调用中使用JSON.stringify()

我对ajax和JSON的了解有限,但我知道在ajax调用中使用JSON.stringify有时会很有用。 我有一个下面的ajax调用工作正常,而它下面的一个使用stringify方法不起作用。 我想知道我是否正确使用.stringify,如果没有,我何时应该在ajax中使用JSON.stringify,如果有的话。 我正在使用MVS与模型,视图和控制器。

这就是我通常做ajax调用的方式,以及我如何构建url部分。

function AddEquipment(id, name, type, description, email) { $.ajax({ url: '@Url.Action("AddEquipment", "Home")' + '/?id=' + id + "&name=" + name + "&type=" + type + "&description=" + description + "&email=" + email, type: "GET", cache: false, datatype: "JSON", success: function(result) { //do stuff } }); } 

下面我尝试使用JSON.stringify而不是手动构建整个URL,但它不起作用。

  function AddEquipment(id, name, type, description, email) { $.ajax({ url: '@Url.Action("AddEquipment", "Home")', type: "GET", cache: false, datatype: "JSON", data: JSON.stringify({ "id": id, "name": name, "type": type, "description": description, "email": email }), success: function(result) { //do stuff } }); } 

控制器方法接受id为int,而其他所有都是字符串。 我之前使用过混合变量(整数,整数,字符串)的JSON.stringify没有问题。

非常感谢任何有用的信息,

谢谢!

这是两个不同的字符串(它们最终评估的值)。 一个不等于其他。 Stringify不会根据您的需要产生’=’。

阅读此文章以传递来电数据

 JSON.stringify({ "id": id, "name": name, "type": type, "description": description, "email": email }), url: '@Url.Action("AddEquipment", "Home")' + '/?id=' + id + "&name=" + name + "&type=" + type + "&description=" + description + "&email=" + email