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.

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序