[英]Read Post Data With C#
I'm Using the Jquery Ajax Function to send data from an html page, & I'm reading it at the backend with C#.我正在使用 Jquery Ajax 函数从 html 页面发送数据,我正在使用 C# 在后端读取它。
When I used Get, the data were sent successfully, but when I switched to Post, the data value is null at the backend.当我使用Get时,数据发送成功,但是当我切换到Post时,后端的数据值为null。
HTML: HTML:
<form onsubmit="AddData();>
<input type="submit"value="Submit" />
</form>
Javascript: Javascript:
function AddData() {
var RData = GetRData();
var postData = { 'Function': "AddData", 'RData': JSON.stringify(RData) };
$.ajax(
{
type: "POST",
url: "/Services/GetRData.aspx",
data: postData,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
jsonp: 'jsoncallback',
success: function (result) {
AddClubCallBackFunction(result) },
error: function (msg) {
alert('Sorry, an error occured while adding your Data!');
}
});
}
C#: C#:
string FunctionName =Request["Function"]; //null
string RData = Request.Form["RData"]; //null
So, what am I doing wrong & how can I fix it ?那么,我做错了什么,我该如何解决?
Update:更新:
I just removed我刚删除
contentType: "application/json; charset=utf-8",
and it worked.它奏效了。
Assuming the above code is exactly what you are executing, there does appear to be a typo:假设上面的代码正是你正在执行的,那么似乎有一个错字:
var Data = GetRData();
var postData = { 'Function': "AddData", 'RData': JSON.stringify(RData) };
Your variable is Data
but your calling JSON.Stringify
on RData
which hasn't been declared (hence null property).您的变量
Data
,但您的电话JSON.Stringify
上RData
尚未宣布(因此空属性)。
Update更新
Based on your last comment I would say it's a JSON formatting issue.根据您的最后一条评论,我会说这是一个 JSON 格式问题。 This can generally happen when you attempt to manually construct JSON objects which you are partly doing in your solution.
当您尝试手动构建您在解决方案中部分执行的 JSON 对象时,通常会发生这种情况。 I would change your code to:
我会将您的代码更改为:
var rdata = GetRData();
var postData = { Function: 'AddData', RData: JSON.stringify(rdata) };
$.ajax({
...
data: JSON.stringify(postData),
...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.