0

こんにちは、ajax jquery を介して Web サービスにデータを送信する際に問題が発生しています。

onclient メソッドは起動しますが、サービスには行きません

ここに私のaspxページコード:

        <script src="Scripts/jquery-1.7.1.js"></script>
        <script type="text/javascript">
            function Start() {
                debugger;
                $.ajax({

                    type: "POST",
                    url: "WebService.asmx/Start",
                    data: '{speakstr: "' + $("#lbl").html() + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        // alert(msg.d);
                    }
                });
                return false;
            }
        </script>
        <title></title>
    </head>

    <body>

        <form id="form1" runat="server"> 
            <div>
                <asp:Label ID="lbl" Text="text" runat="server" />
                <asp:Button ID="Btn" Text="listen" runat="server" OnClientClick="Start()" />
            </div>
        </form>

そして、ここに私の単純な webservice.asmx ページがあります:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public static void Start(string text)
    {
        string txt = text;
    }
}

ありがとう。

4

2 に答える 2

1

これを試してください:コードjsコードを変更してください:

   function Start() {
                $.ajax({
                    type: "POST",
                    url: "WebService.asmx/Start",
                    data: JSON.stringify({"text" : $("#lbl").html()}),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg.d);
                    }
                });
            }

asmx で、メソッドを変更します。

[WebMethod]
public static string Start(string text)
{
    string txt = text;
    return txt; // just for test
}  
于 2013-09-26T20:20:28.580 に答える
0

よくわかりませんが、ajax呼び出しのパラメーター名も「テキスト」にするべきではありませんか? それがあなたのWebメソッドのパラメーターであるためです。

また、よくわかりませんが、[Webmethod] の後に括弧を付けるべきではありませんか?

于 2013-09-26T20:19:30.837 に答える