Ajax调用公共Web服务 [英] Ajax call to a public web service
问题描述
您好,
我正在尝试拨打公开的asmx网络服务() [ ^ ]通过ajax调用来自视觉工作室。
但我无法做到这一点,因为我收到错误状态消息代码为0.
任何人都可以告诉我在哪里出错?
问候,
Swayam
我尝试过:
< html>
< head>
< script src =http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js>< / script>
< script> < br $>
var url =http://www.webservicex.net/globalweather.asmx;
$(document).ready(function (){
$ .ajax({
类型:POST ,
url:url +/ GetCitiesByCountry,
数据:{CountryName:'IND'},
contentType:application / JSON; charset = utf-8,
dataType:json,
成功:OnSuccessCall,
错误:OnErrorCall
});
函数OnSuccessCall(响应){
alert(response.d);
}
函数OnErrorCall(响应){
alert(response.status ++ response。 statusText);
}
});
< / script>
< / head>
< / html>
Hello,
I am trying to call a publicly available asmx web service()[^] by an ajax call from visual studio.
But I am not able to do that as i am getting error status message code as 0.
Can anyone tell me where I am going wrong?
Regards,
Swayam
What I have tried:
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
<script>
var url = "http://www.webservicex.net/globalweather.asmx";
$(document).ready(function () {
$.ajax({
type: "POST",
url: url + "/GetCitiesByCountry",
data: "{CountryName:'IND'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
function OnSuccessCall(response) {
alert(response.d);
}
function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}
});
</script>
</head>
</html>
推荐答案
(文件).ready(function(){
(document).ready(function () {
.ajax({
类型:POST,
url:url + / GetCitiesByCountry,
数据:{CountryName:'IND'},
contentType:application / json; charset = utf-8,
dataType:json,
成功:OnSuccessCall,
错误:OnErrorCall
});
函数OnSuccessCall(响应){
alert(response.d);
}
函数OnErrorCall(响应){
alert(response.status ++ response。 statusText);
}
});
< / script>
< / head>
< / html>
.ajax({
type: "POST",
url: url + "/GetCitiesByCountry",
data: "{CountryName:'IND'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
function OnSuccessCall(response) {
alert(response.d);
}
function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}
});
</script>
</head>
</html>
由于提供的细节有限,很难理解问题所在。但在这里,我基于使用AJAX调用Web服务的一般方法给出了一个想法。
我假设,您尝试调用的服务GetCitiesByCountry需要一个名称参数腠ntryName,我猜问题就在你发送这个参数的地方。尝试更改这样的代码,看看它是否有效。
您可以看到我使用JSON.stringify
在发送到服务之前格式化输入。
With the limited details provided, it is difficult to understand what the issue is. But here, I am giving an idea based on the general way of calling web service using AJAX.
I assume, the service GetCitiesByCountry, you are trying to call expects a parameter of name CountryName and I guess the issue is in the place where you send this parameter. Try changing the code like this and see if it works.
You can see I usedJSON.stringify
for formatting the input before sending to the service.
var url = "http://www.webservicex.net/globalweather.asmx";
这篇关于Ajax调用公共Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!