$.ajax 无法在 IE 中工作,但在 Mozilla 中工作正常
我有以下代码,可以在 Mozilla 上正常运行,但给出对象未定义错误 当在 IE 上运行时,
这里是代码
$(document).ready(function () {
$("#button").click(function () {
$.ajax({
type: "POST",
beforeSend: function (xhr) {
xhr.setRequestHeader("Content-type",
"application/json; charset=utf-8");
},
data: "{'customerid':" + "'" + $("#check").val + "'}",
url: "ajaxcall.aspx/checkval",
success: function (data) {
var c = data.d;
alert("success");
$("#result").html("day:" + c.seconds);
}
});
});
});
,现在我已经更改了服务器端 C# 代码 使用 json 序列化的 checkval 函数
Mydate md = new Mydate();
md.day = DateTime.Now.Day.ToString();
md.month = DateTime.Now.Month.ToString();
md.year = DateTime.Now.Year.ToString();
md.seconds = DateTime.Now.Second.ToString();
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(md);
现在代码可以在 IE 中运行,但仅将字符串显示为
{"月":"10","年":"2011","日":"13","秒":"44"} 并且不与 Mozilaa 合作
如果我只写c,则代替c.seconds 在此代码中
$("#result").html("日:" + c.秒);
它适用于所有浏览器,但结果是
{"月":"10","年":"2011","日":"13","秒":"44"}
仍然没有得到我需要的东西
,这是我最后得到的答案 代替这个
var c = data.d;
alert("success");
$("#result").html("day:" + c.seconds);
写
var mydata = $.parseJSON(data.d);
$("#result").html(mydata.seconds);
这适用于 IE 和 Mozilla
i have a following code which runs fine with Mozilla but give object undefined error
when run on IE
here is the code
$(document).ready(function () {
$("#button").click(function () {
$.ajax({
type: "POST",
beforeSend: function (xhr) {
xhr.setRequestHeader("Content-type",
"application/json; charset=utf-8");
},
data: "{'customerid':" + "'" + $("#check").val + "'}",
url: "ajaxcall.aspx/checkval",
success: function (data) {
var c = data.d;
alert("success");
$("#result").html("day:" + c.seconds);
}
});
});
});
now i have changed my server side c# code inside
checkval function using json serialzation
Mydate md = new Mydate();
md.day = DateTime.Now.Day.ToString();
md.month = DateTime.Now.Month.ToString();
md.year = DateTime.Now.Year.ToString();
md.seconds = DateTime.Now.Second.ToString();
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(md);
Now code is working in IE but only showing the string as
{"month":"10","year":"2011","day":"13","seconds":"44"}
and not working with Mozilaain place of c.seconds if i write c only
in this code$("#result").html("day:" + c.seconds);
it works for all browsers but the result is
{"month":"10","year":"2011","day":"13","seconds":"44"}
Still not getting what i need
here is the answer atlast i got it
in place of this
var c = data.d;
alert("success");
$("#result").html("day:" + c.seconds);
Write
var mydata = $.parseJSON(data.d);
$("#result").html(mydata.seconds);
this will work in IE and Mozilla both
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
data
中可能存在一个小拼写错误:"{'customerid':" + "'" + $("#check").val + "'}"
。您尝试过
$("#check").val()
吗?Perhaps a small typo in
data
:"{'customerid':" + "'" + $("#check").val + "'}"
.Have you tried
$("#check").val()
?