gpt4 book ai didi

c# - jQuery AJAX 调用返回 null 响应时间到时间

转载 作者:太空狗 更新时间:2023-10-30 01:06:39 25 4
gpt4 key购买 nike

我的页面上有一个链接,它创建一个 AJAX 调用页面本身并返回一个值。成功创建的服务器生成的响应变为空或有时是 AJAX 的错误功能。我几乎尝试了所有方法,但无法提出解决方案。

这是我的 AJAX 函数:

var Ajax = {
AjaxCall: function (uri, jsonData, callBack, postParams) {
$.ajax({
cache: false,
type: "POST",
url: uri + ((uri.indexOf("?") > -1) ? "&" : "?") + "rand=" + new Date().format("ssmmHHddMMyyyy"),
data: JSON.stringify(jsonData),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
Ajax.AjaxCall_CallBack(msg, callBack, postParams);
},
error: function (msg) {
Ajax.AjaxCall_CallBack_Error(msg);
}
});
},

AjaxCall_CallBack: function (msg, callBack, postParams) {
callBack(msg, postParams);
},

AjaxCall_CallBack_Error: function (msg) {
alert(msg.responseText);
}
}

并且该函数进行 AJAX 调用

Ajax.AjaxCall('dummypage.aspx?ajax=6&edit=1&uID=' + uID, null, callback_func, [uID, 'Test Text', 0])

从服务器生成的值是这样的:

Table tbl;
TableRow tr;
TableCell tc;
Label lbl;
DropDownList ddl;

tbl = new Table();
tr = new TableRow();

tc = new TableCell();
lbl = new Label();
lbl.Text = "Choose one"
tc.Controls.Add(lbl);
tr.Cells.Add(tc);

tc = new TableCell();
ddl = new DropDownList();
ddl.Items.Add("Choose", "-1"));
ddl.Items.Add("First", "1");
ddl.Items.Add("Second", "2");
ddl.ID = "ddlContentType";

tc.Controls.Add(ddl);
tr.Cells.Add(tc);

tbl.Rows.Add(tr);

StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);

tbl.RenderControl(hw);
string renderedValue = sb.ToString();
hw.Close();

return HttpUtility.JavaScriptStringEncode(renderedValue, true);

我不明白为什么在具有相同过程的同一页面上完全相同的结构有效,但这个却不行?

提前致谢。

最佳答案

正如@gaurav 在 jQuery AJAX Call Returns null Response Time to Time 中提到的那样某些浏览器无法正确处理空数据。我按如下方式处理空数据:

function RunAjaxCall(uri, jsonData, callBack, postParams) {
$.ajax({
type: "POST",
url: uri + ((uri.indexOf("?") > -1) ? "&" : "?") + "rand=" + new Date().getTime(),
**data: jsonData != null ? JSON.stringify(jsonData) : {},**
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: "false",
success: function (msg) {
Success(msg, callBack, postParams);
},
error: function (msg) {
Error(msg);
}
});
},

关于c# - jQuery AJAX 调用返回 null 响应时间到时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14816164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com