通过JQuery调用远程asmx服务总是失败

时间:2013-11-06 12:23:33

标签: ajax web-services jquery

我想使用下面的服务抛出JQuery: http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry 但它只执行错误功能,我在下面尝试过:

        function serviceCall() {
        var txtInput = $("#txtInput").val();
        var webMethod = 'http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry';
        var datap = {"CountryName":JSON.stringify("Italy")}; 

        $("#divResult").html('loading...');

        $.ajax({
            type: "POST",
            url: webMethod,
            data: datap,// { "CountryName" : JSON.stringify("Italy")},
            contentType: "application/json; charset=utf-8",
            dataType: "jsonp", //for Firefox change this to "jsonp"
            success: function (response) {
                alert("reached success");
                $("#divResult").html(response.d);
            },
            error: function (e) {
                $("#divResult").html("Unavailable: " + txtInput);
            }
        });
    }

所以我收到了Unavailable:Italy

以下是整页代码:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
        function serviceCall() {
        var txtInput = $("#txtInput").val();
        var webMethod = 'http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry';
        var datap = {"CountryName":JSON.stringify("Italy")}; 

        $("#divResult").html('loading...');

        $.ajax({
            type: "POST",
            url: webMethod,
            data: datap,// { "CountryName" : JSON.stringify("Italy")},
            contentType: "application/json; charset=utf-8",
            dataType: "jsonp", //for Firefox change this to "jsonp"
            success: function (response) {
                alert("reached success");
                $("#divResult").html(response.d);
            },
            error: function (e) {
                $("#divResult").html("Unavailable: " + txtInput);
            }
        });
    }
</script>

<title></title>
</head>
<body>
<form id="form1" runat="server">
    <input type="text" id="txtInput" value="Italy"/>
    <br />
     <div style="width: 100px; height: 30px; background-color: yellow;" onclick="serviceCall();">
    Click me</div>
<div id="divResult" runat="server">

</div>
</form>
</body>
</html>

有任何帮助解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

我可以在这里看到几个错误:

  • dataType 必须是 json 不是 jsonp
  • 您的有效负载(数据的值)必须是json-object 完全序列化
  • 是您的WebMethod ScriptMethod

虽然无法确切地说出错误。我需要从服务器看到错误消息。

答案 1 :(得分:0)

使用以下示例从其他任何地方调用asmx方法和任何Web方法

    $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: 'PageName.aspx/SaveData',
            data: "{'radio':'" + input 1+ "', 'min':'" + input 2 + "', 'sec':'" + input 3 + "'}",
            async: false,
            success: function (response) {
            },
            error: function ()
            { console.log('there is some error'); }
        });