javascript - 如何在 AJAX URL 中传递参数?

标签 javascript jquery ajax wcf xmlhttprequest

我开发了一项正在成功运行的服务。以下是我的服务代码:

namespace WcfService1
{   
[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method="GET", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate="/display/{a}/{b}")]        
    string Display(string a, string b);        
} 
}

我的服务:

namespace WcfService1
{
 public class Service1 : IService1
{
    public string Display(string a, string b)
    {
        int ab = Convert.ToInt32(a);
        int bc = Convert.ToInt32(b);
        int cb = ab + bc;
        return cb.ToString();
    }
}
}

如何借助 AJAX URL 调用它?我已经尝试了以下代码,但它不起作用。

<script type="text/javascript">
    $(document).ready(function () {
        $('#BtnRegister').click(function () {
            debugger;

            var No1 = document.getElementById('TxtFirstNumber').value;
            var No2 = document.getElementById('TxtSecondNumber').value;

            $.ajax({
                cache: false,
                type: "GET",
                async: false,
                url: "http://localhost:22727/Service1.svc/Display",
                data: 'a=' +No1+'&b='+No2,
                contentType: "application/json; charset=ytf-8",
                dataType: "json",
                processData: true,
                success: function (result) {
                    alert("data");
                },
                error: function (xhr, textStatus, errorThrown) { alert(textStatus + ':' + errorThrown); }
            });
        });
    });
</script>

更新:

我将代码更改为以下代码:

$(document).ready(function () {
        $('#BtnRegister').click(function () {
            debugger;

            var No1 = document.getElementById('TxtFirstNumber').value;
            var No2 = document.getElementById('TxtSecondNumber').value;

            $.ajax({
                cache: false,
                type: "GET",
                url: "http://localhost:22727/Service1.svc/Display",
                data: { a: No1, b: No2 },
                contentType: "application/json; charset=ytf-8",
                //processData: true, //<-- this isn't needed because it defaults to true anyway
                success: function (result) {
                    alert("data");
                },
                error: function (xhr, textStatus, errorThrown) { alert(textStatus + ':' + errorThrown); }
            });
        });
    });

但是当我点击按钮时:

enter image description here

最佳答案

试试这个:

$.ajax({
   type:'GET',
   url:'your url',
   data:{ var1: value1, var2: value2},
   success:function(response)
{

}

});

并在业务层上,使用变量名称检索它们,即。 var1 和 var2。

关于javascript - 如何在 AJAX URL 中传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180785/

相关文章:

javascript - 悬停时停止和开始视频

javascript - 在全日历中禁用外部事件的突出显示

javascript - Javascript 到 Rails AJAX 连接时出现错误 "Not Found"

jquery - Django Ajax 。 dajax 仍然可以吗?还是最好立即转向 jQuery(2012 年复兴)?

ajax - 在 Aurelias HTTP 客户端中捕获错误

javascript - 使用主屏幕图标会破坏 iPhone 上的设备高度

javascript - JavaScript 中的闭包并运行一次函数

jquery委托(delegate)传递变量

javascript - Instafeed.js 不在页面上显示图像

javascript - AngularJS 判断元素是否存在