[英]Jquery Ajax POST Request sending data issue
I have searched a lot and not been able to find a working solution to why my post request is not sending it's data to the server. 我进行了很多搜索,但无法找到有效的解决方案来解释为什么我的发帖请求没有将其数据发送到服务器。 I can send the request without data and I get my results from the server, but I just cannot send my data to the server. 我可以发送不带数据的请求,并且可以从服务器获取结果,但是我无法将数据发送至服务器。 I have narrowed it down to the "data" attribute and assume I am just doing something wrong. 我将其范围缩小到“数据”属性,并假设我做错了什么。 Thank you. 谢谢。
Client 客户
var scriptURL = "default/scripts/serverside/Scripts.aspx";
$.ajax({
type: "POST",
url: baseURL + scriptURL + "/SaveItem",
data: "{}", //works (to return a result)
//data: "{sendData: '" + dataPackage + "'}", //does not work
//data: dataPackage, //does not work
//data: { sendData: dataPackage }, //does not work
//data: { "sendData": dataPackage }, //does not work
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
de("server result:" +result);
}
});
Server 服务器
[WebMethod]
public static string SaveItem(string sendData)
{
string result = "received: " + sendData;
return result;
}
Please help, I just cant seem to get it working and know it has got to be a syntax issue... 请帮助,我似乎无法正常工作,并且知道它必须是语法问题...
Similar problems I have found (but no working answers): 我发现了类似的问题(但没有有效的答案):
Try this one: 试试这个:
$.ajax({
type: "POST",
url: baseURL + scriptURL + "/SaveItem",
data: $.toJSON({ sendData: dataPackage }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
de("server result:" +result);
}
});
The toJSON
will convert your JS object into a proper JSON string. toJSON
将您的JS对象转换为正确的JSON字符串。 You could also use JSON.stringify
您也可以使用JSON.stringify
Try this: 尝试这个:
var scriptURL = "default/scripts/serverside/Scripts.aspx";
$.ajax({
type: "POST",
url: baseURL + scriptURL + "/SaveItem",
data: {sendData: "string to send" }
dataType: "json",
success: function (result) {
de("server result:" +result);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.