使用 jquery ajax asp.net 4.0 调用 asmx 服务
我正在尝试使用 jquery 调用示例 asmx 服务,这里是 jquery 代码
$.ajax({
type: "POST",
url: "/Services/Tasks.asmx/HelloWorld",
data: "{}",
dataType: "json",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);
}
});
这没有显示任何消息,代码位于 asp.net 4.0 中, 我错过了什么吗?
编辑 - 我将 dataType 更改为 xml,现在成功函数正在工作,它返回以下 xml
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
我正在使用以下代码来解析 xml 数据,它在警报中显示 null
success: function (data) {
edata = $(data).find("string").html();
alert(data);
}
I'm trying to call a sample asmx service using jquery, here is the jquery code
$.ajax({
type: "POST",
url: "/Services/Tasks.asmx/HelloWorld",
data: "{}",
dataType: "json",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);
}
});
This is not showing any message,code is in asp.net 4.0,
Am I missing any thing?
Edit - I changed the dataType to xml, now success function is working it return following xml
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
I'm using following code to parse xml data and it is showing null in alert
success: function (data) {
edata = $(data).find("string").html();
alert(data);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信这是因为您有 dataType: "json" 并且它期望响应内容类型相同,但正在返回 XML。我敢打赌整个事件正在发起,但没有成功。
尝试
更新
我认为这是因为你正在使用 .html(),你需要使用 text()。另外,我不知道您是否打算这样做,但您的警报中有
data
,我假设您打算使用edata
。以下对我有用:I believe it's because you have the dataType: "json" and it's expecting the response content-type to be the same but XML is being returned. I bet the complete event is being raised but not success.
try
UPDATE
I think it's because you're using .html(), you need to use text(). Also i don't know if you meant to do it or not but you have
data
in your alert, i'm assuming you meant to useedata
. The following worked for me:我建议将 [ScriptService] 属性添加到您的 Tasks.asmx 类中,以便它将以 JSON 而不是 XML 的形式接受和响应。您的客户端代码看起来不错,但您需要查看成功处理程序中的“data.d”而不是“data”。
I'd recommend adding the [ScriptService] attribute to your Tasks.asmx class so it will accept and respond in JSON instead of XML. Your client code looks good, but you'll want to take a look at "data.d" instead of "data" in your success handler.
好吧,您声明 dataType 是 JSON,但 contentType 是 XML。尝试
如果没有,那么我们就必须查看 asmx 代码。
Well, you're stating that the dataType is JSON, but the contentType is XML. Try
If not, then we'd have to see the asmx code.