繁体   English   中英

jquery ajax 将参数传递给webservice

[英]jquery ajax pass parameters to webservice

我必须在 Web 服务中运行,其中一个可以正常工作,接收两个参数,我可以执行它,但是另一个也接收参数的不能工作,粘贴这个函数和我的 Ajax 代码,这样你就可以帮我看看是什么正在发生。

html

<script type="text/javascript">
    function CallService() {
        $.ajax({
            type: "POST",
            url: "findMe.asmx/locateMe2",  
            crossDomain:true,
            data: '{value1: ' + $("#txtValue1").val() + ', value2: ' + $("#txtValue2").val() + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnError
        });

    }

    function OnSuccess(data, status) {
        $("#lblResult").html(data.d);

    }

    function OnError(request, status, error) {
        $("#lblResult").html(request.statusText);

    }


</script>    

</head>
<body>
<form id="frmCoords" runat ="server" >
    <div>
<table>
    <tbody>
        <tr>
            <th>
                Value 1:
            </th>
            <td>
                <asp:TextBox ID="txtValue1" runat="server" />
            </td>
        </tr>
        <tr>
            <th>
                Value 2:
            </th>
            <td>
                <asp:TextBox ID="txtValue2" runat="server" />
            </td>
        </tr>
    </tbody>
</table>

<asp:Button ID="btnGo" Text="Go" OnClientClick="CallService(); return false;" runat="server" />

<asp:Label ID="lblResult" Text="&nbsp;" Width="100%" runat="server" ForeColor="black"  />
    </div>

</form>


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


    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

    public int Add(int value1, int value2)
    {
        return value1 + value2;
    }

    public string locateMe2(Double value1, Double value2)
    {

        FormClosingEventArgs ee = new FormClosingEventArgs(CloseReason.UserClosing, false);
        DialogResult dlgResult = MessageBox.Show("", "Cadena", MessageBoxButtons.OK, MessageBoxIcon.Information);

        SqlConnection Conn = new SqlConnection();
        Conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Audi.Properties.Settings.ConexionSql"].ConnectionString);
        string procedure = "usp_PointInPolygon";

        SqlCommand cmd = new SqlCommand(procedure, Conn);
        SqlDataReader reader;


        //cmd.CommandText = "usp_PointInPolygon";
        cmd.CommandType = CommandType.StoredProcedure;
        //cmd.Parameters.Clear();
        SqlParameter param1;
        SqlParameter param2;
        param1 = cmd.Parameters.Add("@lat", SqlDbType.Float,14);
        param2 = cmd.Parameters.Add("@lng", SqlDbType.Float,14);

        param1.Value = value1;
        param2.Value = value2;

        Conn.Open();

        reader = cmd.ExecuteReader();
        string column = "";
        while (reader.Read())
        {
            column = reader["county"].ToString();
            //int columnValue = Convert.ToInt32(reader["ColumnName"]);
        }
        Conn.Close();

        return column;

    }

函数 Add 工作正常,它接收两个 int 值,函数 locateMe2 也接收 lat 和 lng 并且是浮点值的值不起作用,你发现有什么问题吗?

the locateMe2 function will return just a string
var params = {value1: $("#txtValue1").val(), value2: $("#txtValue2").val()};
function CallService() {
  $.ajax({
        type: "POST",
        url: "findMe.asmx/locateMe2",  
        crossDomain:true,
        data: JSON.stringify(params),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        error: OnError
    });
}

值可以直接作为字符串放入“数据”设置中将值设置在 ajax 方法之外有助于监控您在从变量或元素获取值时特别编写的内容,而“stringify”方法有助于将值放入字符串形状为这是必需的。

暂无
暂无

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

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