【问题标题】:Consuming an asmx web service using ajax/jquery in ASP.NET?在 ASP.NET 中使用 ajax/jquery 使用 asmx Web 服务?
【发布时间】:2010-08-09 05:09:12
【问题描述】:

嗨, 我正在尝试使用 Web 服务、jquery 和 json 保存数据。从网上的一些例子中,我得到了一个类似的代码,但我使用的是字符串而不是 int:

HTML:

<div id="dialogL" title="Create a new Layer">
    <p>Enter Name:</p>
    <input id="txtValue1" type="text" size="40" />
    <p>Enter Description:</p>
    <textarea id="txtValue2" rows="4" cols="30"></textarea>
    <br />
    <input type="button" value="Create" onclick="Save()" /> 

JavaScript:

<script type="text/javascript">
    function Save() { 
        $.ajax({
            type: "POST",
            url: "testservice1.asmx/Save",
            data: "{ 'value1': " + $("#txtValue1").val() + ", 'value2': " + $("#txtValue2").val() + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: Success,
            error: Error
        });
    }

    function Success(data, status) {

    }

    function Error(request, status, error) {
        alert("Error: " + request.statusText);
    }
</script>

ASP.NET

    [WebMethod]
    public bool Save(String value1, String value2)
    {
       DoSave();
       return true;
    }

但这没有用,并给了我一个内部错误消息。我做错了什么?

【问题讨论】:

    标签: jquery asp.net ajax webmethod


    【解决方案1】:

    更改您的数据部分,改为使用

    data: "{ value1: '" + $("#txtValue1").val() + "', value2: '" + $("#txtValue2").val() + "'}",
    

    【讨论】:

    • 这里我没有给参数加上引号,而是给对应的值加上引号。
    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 2011-03-31
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2013-02-11
    • 2013-12-17
    相关资源
    最近更新 更多