JQuery AJAX语法

Nic*_*ick 8 ajax jquery post web-services

我试图找到正确的语法将varible传递给我的JQuery Post.

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);
    }
Run Code Online (Sandbox Code Playgroud)

我不认为数据:价值是非常正确的.有人让我直截了当?

谢谢!

Jos*_*lio 15

这个怎么样:

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);
    }
});
Run Code Online (Sandbox Code Playgroud)


Pao*_*ino 6

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

data: {empid: empid},
Run Code Online (Sandbox Code Playgroud)

要么

data: "empid=" + empid,
Run Code Online (Sandbox Code Playgroud)

文档说:

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


小智 5

完成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){

            }

        }); 
Run Code Online (Sandbox Code Playgroud)