JQuery AJAX Syntax

I am trying to find the correct syntax for passing varible to 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);
    }

I do not think the data: the meaning is absolutely true. Has someone set me straight?

Thanks!

+5
source share
9 answers

How about this:

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);
    }
});
+9
source

data can be either an encoded URL string or an object:

data: {empid: empid},

OR

data: "empid=" + empid,

The docs say:

. , . URL- GET. . processData . "/". , jQuery , {foo: [ "bar1", "bar2" ]} '& foo = bar1 & foo = bar2'.

+4

.

$.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);
}
+2

. , , .

data: {"empid" : empid}

? , empid - - , .

data: "empid="+empid

http://docs.jquery.com/Ajax/jQuery.ajax#options

+1

, , jquery

$. proxy()

Proxy .

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

CODE

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);}
                      );

+1
  $(document).ready(function() {
  $.ajax({
    type: "POST",
    url: "Webservices/EmployeeService.asmx/GetEmployeeOrders",
    data: "{'EmployeeId':'empid'}", **<-- see the single quotes**
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
          alert(msg);
         }
  });
});
0

JSON

data: "{empid: " + empid + "}"

querystring (? empid = 123)

data: {empid : empid}
0

.

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);
    }
0

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

            }

        }); 
0

All Articles