Click here to Skip to main content
15,905,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>
Posted
Updated 22-Mar-16 12:31pm
Comments
Kornfeld Eliyahu Peter 22-Mar-16 17:03pm    
According to samples HTTP POST does not support JSON as data format, but url-encoded only...

1 solution

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 used JSON.stringify for formatting the input before sending to the service.

JavaScript
var url = "http://www.webservicex.net/globalweather.asmx";

$(document).ready(function () {
 var input = { CountryName: "IND" };
$.ajax({
type: "POST",
url: url + "/GetCitiesByCountry",
data: JSON.stringify(input),
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);
}
});
 
Share this answer
 
Comments
swayamrath 23-Mar-16 5:59am    
Thanks Mani.
http://www.webservicex.net/globalweather.asmx is a publicly available web service.
GetCitiesByCountry is one of the methods in the web service.

This method takes country name (For Example - IND) to give am XML output.

I used your code and ran it. But it's still going to the error block.

Any suggestion?

Regards,
Swayam
Mathi Mani 23-Mar-16 13:58pm    
The example is using JSON, but you are expecting XML. Try changing the dataType to XML and see what happens.
swayamrath 24-Mar-16 3:11am    
I tried that as well. But not sure why this is not happening.
<script>
var url = "http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry";

$(document).ready(function () {
var input = {CountryName:"IND"};
$.ajax({
type: "POST",
url: url,
data: input,
crossDomain:true,
contentType: "application/soap+xml; charset=utf-8",

dataType: "xml",
success: OnSuccessCall,
error: OnErrorCall
});

function OnSuccessCall(response) {
alert(response.d);
}

function OnErrorCall(xhr, ajaxOptions, thrownError) {
alert(xhr);
}
});
</script>

It's getting into the error block. I think it has to do with the CORS issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900