【问题标题】:JQuery AJAX syntaxjQuery AJAX 语法
【发布时间】:2009-04-23 02:02:21
【问题描述】:

我正在尝试找到正确的语法来将变量传递给我的 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);
    }

我不认为 data: value 是完全正确的。有人直截了当?

谢谢!

【问题讨论】:

    标签: jquery ajax web-services post


    【解决方案1】:

    这个怎么样:

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

    【讨论】:

    • 我正在尝试通过邮递员发送 ajax 请求。 ajax 请求正在发送 dataType:`json` 和 data: {loginId: "appletest@somedomain.com", client: "698983"}。在进入邮递员时,我试图将正文参数作为 JSON 发送,其中包含所有上述信息和 Content-Type: application/json in headers,但它失败并显示 500。有帮助吗?
    【解决方案2】:

    完整的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){
    
                }
    
            }); 
    

    【讨论】:

    • 运行良好。谢谢
    【解决方案3】:

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

    data: {empid: empid},
    

    data: "empid=" + empid,
    

    文档说:

    要发送到服务器的数据。如果还不是字符串,则将其转换为查询字符串。它附加到 GET 请求的 url。请参阅 processData 选项以防止此自动处理。对象必须是键/值对。如果 value 是一个数组,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);
      }
      

      【讨论】:

        【解决方案5】:

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

        data: {"empid" : empid}
        

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

        data: "empid="+empid
        

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

        【讨论】:

          【解决方案6】:

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

          $.proxy() Method

          Proxy 方法采用现有函数并返回具有特定上下文的新函数。

          语法

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

          参考文献

          1. $(this) inside of AJAX success not working
          2. jQuery Cross-Domain AJAX Request methods

          【讨论】:

            【解决方案7】:
              $(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);
                     }
              });
            });
            

            【讨论】:

              【解决方案8】:

              如果你想向服务器发送一个 JSON 字符串

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

              如果您想发送查询字符串参数 (?empid=123)

              data: {empid : empid}
              

              【讨论】:

                【解决方案9】:

                您可以使用以下内容。

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

                【讨论】:

                  【解决方案10】:
                  $(文档).ready(函数(){ 获取数据(); });
                  function getdata(){
                      $.ajax({
                      url:'sample.html',
                      type:'get',
                      dataType:'text',
                      success: succesfun,
                      error: errorfun,
                      complete:function(xhr, status){
                          console.log("your function is success");
                      }
                  });
                  }
                  function succesfun(hello){
                      console.log("Success");
                      $('#result').append(hello);
                  }
                  function errorfun(){
                      console.log("Error");
                  }
                  

                  【讨论】:

                  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-01-11
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-10-20
                  • 1970-01-01
                  • 2018-06-20
                  • 1970-01-01
                  相关资源
                  最近更新 更多