JQuery AJAX syntax

前端 未结 9 1869
旧时难觅i
旧时难觅i 2021-01-17 20:11

I am trying to find the correct syntax to pass a varible to my JQuery Post.

var id = empid;

$.ajax({
    type: \"POST\",
    url: \"../Webservices/EmployeeS         


        
相关标签:
9条回答
  • 2021-01-17 20:42

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

    data: {empid: empid},
    

    OR

    data: "empid=" + empid,
    

    The docs say:

    Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key i.e. {foo:["bar1", "bar2"]} becomes '&foo=bar1&foo=bar2'.

    0 讨论(0)
  • 2021-01-17 20:42

    Though not a direct answer to your question, following is the common function approach used in one of our projects for jquery calls

    $.proxy() Method

    The Proxy method takes an existing function and returns a new one with a particular context.

    Syntaxes

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

    REFERENCES

    1. $(this) inside of AJAX success not working
    2. jQuery Cross-Domain AJAX Request methods
    0 讨论(0)
  • 2021-01-17 20:45

    It's not. You're passing a string, you should be passing an object literal,e.g.

    data: {"empid" : empid}
    

    See the difference? Assuming empid is a variable with some sort of value, that should work fine. Alternatively you can do this

    data: "empid="+empid
    

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

    0 讨论(0)
  • 2021-01-17 20:57

    This should work for you.

    $.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);
    }
    
    0 讨论(0)
  • 2021-01-17 20:57

    if you want to send a JSON string to the server

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

    if you want to send querystring params (?empid=123)

    data: {empid : empid}
    
    0 讨论(0)
  • 2021-01-17 20:59
      $(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 讨论(0)
提交回复
热议问题