簡體   English   中英

如果更改簽名中的返回類型,為什么不調用Web服務?

[英]Why web service not called if I change return type in signature?

我創建了asp.net Web服務,並借助jquery ajax對其進行了調用。

這是jquery ajax方法:

  $.ajax( {         
      type: "POST",     
      url: "../../Search/Address.aspx/Search2",
      contentType: "application/json; charset=utf-8",
      dataType: "json", 
      data: '{ txtStreet:"'+ "bronson str." +'"}', 
      success: function( data ) {
       alert(data)
       //response(data.d);
      },
       error: function (error) {
         alert("error")
        }
    }) ;

而且我有這個asp.net網絡服務:

public partial class Address : Page
{
    //some enother  functions and events.

    [System.Web.Services.WebMethodAttribute, System.Web.Script.Services.ScriptMethodAttribute]
    public static string[] Search2(string txtStreet) 
    {
        return new String[] { "Foo1", "Foo2", "Foo3" }; ;
    }
}

上面的服務很好!

如果在上面的服務中我更改了簽名中的返回類型,則不會調用它:

public partial class Address : Page
{
    //some enother  functions and events.

    [System.Web.Services.WebMethodAttribute, System.Web.Script.Services.ScriptMethodAttribute]
    public static string Search2(string txtStreet) 
    {
        return "Foo1";
    }
}

知道為什么更改簽名中的返回類型時未調用服務嗎?

上面的代碼應該可以正常工作,這是我的一個有效示例:

<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>

<script type="text/javascript">
        $(document).ready(function () {
            $("#txtValue").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "GetJsonData.aspx/GetValue",
                        data: "{'value':'" + $("#txtValue").val() + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data);
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        });
</script>

<div>
    <input type="text" id="txtValue" />
</div>

網絡方法

[WebMethod]
public static string GetValue(string value)
{
   return ("Foo1");
}

要么

[System.Web.Services.WebMethodAttribute, System.Web.Script.Services.ScriptMethodAttribute]
 public static string GetValue(string value)
 {
    return ("Foo1");
 }

在Jquery中,將dataType設置為“ Json”,在Webservice中,您將返回字符串。 如果要在ajax調用中將數據類型設置為json,請傳遞有效的json。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM