使用jquery ajax的asp.net 4.0调用ASMX服务 [英] calling asmx service using jquery ajax asp.net 4.0
问题描述
我试图使用jQuery调用示例ASMX服务,这里是jQuery的code
$。阿贾克斯({
键入:POST,
网址:/Services/Tasks.asmx/HelloWorld
数据:{},
数据类型:JSON
的contentType:应用程序/ xml的;字符集= UTF-8,
成功:功能(数据){
警报(数据);
}
});
这是没有显示的任何消息,code是在asp.net 4.0中,
我失去了什么东西?
编辑 - 我改变了数据类型为XML,现在成功的功能正在工作寄回以下XML
<?XML版本=1.0编码=UTF-8&GT?;
<字符串的xmlns =http://tempuri.org/>的Hello World< /串>
我用下面的code解析XML数据,它显示在警报空
成功:功能(数据){
。EDATA = $(数据).find(字符串)HTML();
警报(数据);
}
我相信这是因为你有数据类型:JSON,它的预期的响应内容类型是相同的,但被返回的XML。我敢打赌,完整的事件被提出,但没有成功。
尝试
$。阿贾克斯({
键入:POST,
网址:/Services/Tasks.asmx/HelloWorld
数据:{},
数据类型:JSON
的contentType:应用程序/ xml的;字符集= UTF-8,
成功:功能(数据){
警报(数据);
},
完成:功能(数据){
警报(数据);
}
});
更新
我想这是因为你使用的.html(),您需要使用文本()。此外,我不知道,如果你的意思是做与不做,但你必须数据
在你的警觉,我假设你想用 EDATA
。下面为我工作:
jQuery.ajax({
键入:POST,
网址:/ yourURL的,
数据类型:XML,
数据:{},
的contentType:应用程序/ xml的;字符集= UTF-8,
成功:功能(数据){
。EDATA = $(数据).find(字符串)文本();
警报(EDATA);
}
})
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);
}
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
$.ajax({
type: "POST",
url: "/Services/Tasks.asmx/HelloWorld",
data: "{}",
dataType: "json",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);
},
complete: function (data) {
alert(data);
}
});
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 use edata
. The following worked for me:
jQuery.ajax({
type: "POST",
url: "/yourURL",
dataType: "xml",
data: "{}",
contentType: "application/xml; charset=utf-8",
success: function(data) {
edata = $(data).find("string").text();
alert(edata);
}
})
这篇关于使用jquery ajax的asp.net 4.0调用ASMX服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!