weixin_33724059 2015-01-11 17:50 采纳率: 0%
浏览 39

使用WebService的Jquery Ajax

I am trying to create an AJAX Call to a webservice but nothing happens. I want to put a number in an input box submit it to the webservice, calculate it and submit it back to another input box.

$(document).ready(function() {
    $('#Num').change(function(){
        $.ajax({
            type: "GET",
            url: "test5handler.ashx",
            data: { Num: $('#Num').val() },
            error: {alert("Something went wrong")},
            success: function(msg){
                $('#resultNum').val(msg);
            }
        });
    });
});

<form id="form1" runat="server">
<div>
    Please enter a Number:
    <input type="text" id="Num">
    <br />
    <input type="text" id="resultNum">
</div>
</form>

public class test5handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string Num = context.Request.QueryString["Num"];
        Double adjNum = Double.Parse(Num);
        Double Total = (adjNum*5);
        context.Response.Write(Total);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
  • 写回答

1条回答 默认 最新

  • weixin_33720956 2015-01-11 17:53
    关注

    You used:

    $('#searchip')
    

    but I cannot see any element in your DOM with id="searchip".

    I guess you meant:

    $('#Num')
    

    because that's the id of your input field.

    Also I would replace this:

    data: 'Num=' + $('#Num').val(),
    

    with:

    data: { Num: $('#Num').val() },
    

    to ensure proper encoding of the value.

    评论

报告相同问题?