简体   繁体   English

在ASP.NET中使用ajax / jquery消费asmx Web服务吗?

[英]Consuming an asmx web service using ajax/jquery in ASP.NET?

Hi, I am trying to save data using web services, jquery and json. 嗨,我正在尝试使用Web服务,jquery和json保存数据。 From some example on the web I got a similar code but I'm using a String instead of int: 从网络上的一些示例中,我得到了类似的代码,但是我使用的是String而不是int:

HTML: 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: 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 ASP.NET

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

But this didnt work and gave me back an internal error message. 但这没有用,并给了我一个内部错误消息。 What did I do wrong? 我做错了什么?

更改数据部分,改为使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM