【问题标题】:unable to send data to webservice through jquery无法通过jquery向webservice发送数据
【发布时间】:2013-09-26 20:14:38
【问题描述】:

您好,我在通过 ajax 向 Web 服务发送数据时遇到问题 jQuery。

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;
    }
}

谢谢。

【问题讨论】:

    标签: jquery asp.net web-services


    【解决方案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
    }  
    

    【讨论】:

    • 网址可能有误... url: "WebService.asmx/Start" 你确定是正确的 url 吗?我认为可以是这样的:url:“localhost:8888/WebService.asmx/Start”
    • 嗨我试过''function Start() { debugger; $.ajax({ type:"POST", url: "localhost:50417/WebService.asmx/Start", data:JSON.stringify({ "text": $("#lbl").html() }), contentType:"application/json ; charset=utf-8", dataType: "json", 成功: function (msg) { alert(msg.d); } }); }'' 但不是它没有去服务。
    • 你有萤火虫吗?使用 firebug 在控制台选项卡中查看您的帖子会发生什么。
    • '[InvalidOperationException: No web service found at: /SpeechSite/WebService.asmx.]'
    • 所以,就是这样!您必须输入正确的“url”参数。尝试使用浏览器访问网络服务。
    【解决方案2】:

    我不太确定,但不应该在 ajax 调用中使用参数名称 也是“文字”吗?因为那是你web方法的参数。

    另外,我也不太确定,但 [Webmethod] 后面不应该有括号吗?

    【讨论】:

    • 属性中不需要括号。
    • 等一下,我想我以前经历过。甚至在附加到 clientclick 的 javascript 尚未完成之前,asp.net 按钮往往会进行回发。页面是否刷新?您可以尝试使用普通的 html ?
    • 我添加了“OnClientClick="Start();return false;”所以我认为页面不会刷新。
    猜你喜欢
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多