JQuery AJAX语法

JQuery AJAX语法,jquery,ajax,web-services,post,Jquery,Ajax,Web Services,Post,我正在试图找到正确的语法,以便将变量传递给我的JQuery帖子 var id = empid; $.ajax({ type: "POST", url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders", data: "{empid: empid}", contentType: "application/json; charset=utf-8", dataType: "json", su

我正在试图找到正确的语法,以便将变量传递给我的JQuery帖子

var id = empid;

$.ajax({
    type: "POST",
    url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
    data: "{empid: empid}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result) {
        alert(result.d);
    }
我不认为数据:价值是完全正确的。有人帮我纠正了吗

谢谢

可以是URL编码的字符串或对象:

data: {empid: empid},

文件说:

要发送到服务器的数据。如果尚未转换为字符串,则会将其转换为查询字符串。它被附加到GET请求的url。请参阅processData选项以防止此自动处理。对象必须是键/值对。如果value是一个数组,jQuery用相同的键序列化多个值,即{foo:[“bar1”,“bar2”]}变成“&foo=bar1&foo=bar2”


这应该对你有用

$.ajax({
    type: "POST",
    url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
    data: {empid: empid},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result) {
        alert(result.d);
}
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“Webservices/EmployeeService.asmx/GetEmployeeOrders”,
数据:“{'EmployeeId':'empid'}”,***这个怎么样:

var id = empid;

$.ajax({
    type: "POST",
    url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
    data: "{empid: " + empid + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result){
        alert(result.d);
        console.log(result);
    }
});

不是。你在传递一个字符串,你应该传递一个对象文本,例如

data: {"empid" : empid}
看到区别了吗?假设empid是一个具有某种值的变量,这应该可以正常工作。或者您也可以这样做

data: "empid="+empid

如果要向服务器发送JSON字符串

data: "{empid: " + empid + "}"
如果要发送查询字符串参数(?empid=123)


您可以使用以下命令

var id = empid;

$.ajax({
    type: "POST",
    url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
    data: "var1=val1&var2=val2&var3=val3&var4=val4&var5=val5",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        alert(result.d);
    }

虽然不能直接回答您的问题,但以下是我们的一个项目中用于jquery调用的常用函数方法

代理方法接受现有函数并返回具有特定上下文的新函数

语法

$(selector).proxy(function,context)
$(selector).proxy(context,name)  
代码

dpInvokeAsync: function (serviceRequest, input, requestType, successCallBack) {
        var url = BASE_URL + serviceRequest;
        $.ajax({
            type: requestType,
            url: url,
            async: true,
            data: input,
            dataType: 'json',
            success: $.proxy(successCallBack, this),
            error:  $.proxy(this.handleFailure, this)
        });
    }


   this.dpInvokeAsync('App/ShowParts', searchCriteria, 'Post',
                      function (result) { alert(result);}
                      );
参考资料


  • 完整的ajax语法

    var data="abc";
           $.ajax({
                type: "GET",
                url: "XYZ",
                data: {
                    "data":data,
                },
                dataType: "json",
    
                //if received a response from the server
                success: function( datas, textStatus, jqXHR) {
    
                },
    
                //If there was no resonse from the server
                error: function(jqXHR, textStatus, errorThrown){
    
                },
    
                //capture the request before it was sent to server
                beforeSend: function(jqXHR, settings){
    
                },
    
                //this is called after the response or error functions are finished
                //so that we can take some action
                complete: function(jqXHR, textStatus){
    
                }
    
            }); 
    

    我正试图通过postman发送ajax请求。ajax请求正在发送数据类型:`json`和数据:{loginId:'appletest@somedomain.com,client:“698983”}。当我进入邮递员界面时,我试图以JSON的形式发送正文参数,并在标题中包含所有上述信息和内容类型:application/JSON,但失败了,返回500。有什么帮助吗?
    $(selector).proxy(function,context)
    $(selector).proxy(context,name)  
    
    dpInvokeAsync: function (serviceRequest, input, requestType, successCallBack) {
            var url = BASE_URL + serviceRequest;
            $.ajax({
                type: requestType,
                url: url,
                async: true,
                data: input,
                dataType: 'json',
                success: $.proxy(successCallBack, this),
                error:  $.proxy(this.handleFailure, this)
            });
        }
    
    
       this.dpInvokeAsync('App/ShowParts', searchCriteria, 'Post',
                          function (result) { alert(result);}
                          );
    
    var data="abc";
           $.ajax({
                type: "GET",
                url: "XYZ",
                data: {
                    "data":data,
                },
                dataType: "json",
    
                //if received a response from the server
                success: function( datas, textStatus, jqXHR) {
    
                },
    
                //If there was no resonse from the server
                error: function(jqXHR, textStatus, errorThrown){
    
                },
    
                //capture the request before it was sent to server
                beforeSend: function(jqXHR, settings){
    
                },
    
                //this is called after the response or error functions are finished
                //so that we can take some action
                complete: function(jqXHR, textStatus){
    
                }
    
            });