简体   繁体   English

jQuery .ajax() - 向 POST 请求添加查询参数?

[英]jQuery .ajax() - add query parameters to POST request?

To add query parameters to a url using jQuery AJAX, you do this:要使用 jQuery AJAX 向 url 添加查询参数,请执行以下操作:

$.ajax({
  url: 'www.some.url',
  method: 'GET',
  data: {
      param1: 'val1'
  }
)}

Which results in a url like www.some.url?param1=val1这会导致像www.some.url?param1=val1这样的 url

How do I do the same when the method is POST?当方法是 POST 时,我如何做同样的事情? When that is the case, data no longer gets appended as query parameters - it instead makes up the body of the request.在这种情况下, data不再作为查询参数附加 - 而是构成请求的正文。

I know that I could manually append the params to the url manually before the ajax request, but I just have this nagging feeling that I'm missing some obvious way to do this that is shorter than the ~5 lines I'll need to execute before the ajax call.我知道我可以在 ajax 请求之前手动将参数附加到 url,但我只是有一种唠叨的感觉,我错过了一些明显的方法来做到这一点,它比我需要执行的 ~5 行短在ajax调用之前。

jQuery.param() allows you to serialize the properties of an object as a query string, which you could append to the URL yourself: jQuery.param()允许您将对象的属性序列化为查询字符串,您可以自己将其附加到 URL:

$.ajax({
  url: 'http://www.example.com?' + $.param({ paramInQuery: 1 }),
  method: 'POST',
  data: {
    paramInBody: 2
  }
});

Thank you @Ates Goral for the jQuery.ajaxPrefilter() tip.感谢@Ates Goral 提供jQuery.ajaxPrefilter()技巧。 My problem was I could not change the url because it was bound to kendoGrid and the backend web API didn't support kendoGrid 's server paging options (ie page , pageSize , skip and take ).我的问题是我无法更改url因为它绑定到kendoGrid并且后端 Web API 不支持kendoGrid的服务器分页选项(即pagepageSizeskiptake )。 Furthermore, the backend paging options had to be query parameters of a different name.此外,后端分页选项必须是不同名称的查询参数。 So had to put a property in data to trigger the prefiltering.所以不得不在data放置一个属性来触发预过滤。

var grid = $('#grid').kendoGrid({
    // options here...
    dataSource: {
        transport: {
            read: {
                url: url,
                contentType: 'application/json',
                dataType: 'json',
                type: httpRequestType,
                beforeSend: authentication.beforeSend,
                data: function(data) {
                  // added preFilterMe property
                  if (httpRequestType === 'POST') {
                      return {
                          preFilterMe: true,
                          parameters: parameters,
                          page: data.page,
                          itemsPerPage: data.pageSize,
                      };
                  }

                  return {
                      page: data.page,
                      itemsPerPage: data.pageSize,
                  };
              },
          },
        },
    },
});

As you can see, the transport.read options are the same options for jQuery.ajax() .如您所见, transport.read选项与jQuery.ajax()选项相同。 And in the prefiltering bit:在预过滤位中:

$.ajaxPrefilter(function(options, originalOptions, xhr) {
    // only mess with POST request as GET requests automatically
    // put the data as query parameters
    if (originalOptions.type === 'POST' && originalOptions.data.preFilterMe) {
        options.url = options.url + '?page=' + originalOptions.data.page 
            + '&itemsPerPage=' + originalOptions.data.itemsPerPage;

        if (originalOptions.data.parameters.length > 0) {
            options.data = JSON.stringify(originalOptions.data.parameters);
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM