在请求体中而不是在查询字符串中发送参数

时间:2020-12-17 23:56:17

I have a problem with this method:

我对这种方法有一个问题:

 $.ajax({
      url: '/SalesOfferInvoiceDeliveryNote/InsertUpdateCAORAC/?cTa=b&cHTML=' + cHTML,  
      type: 'POST',
      data: $(this).parents('form').serialize()
//other part of method not shown, it's not relevant

On the server side method's signature looks like this:

在服务器端方法的签名如下:

 public string InsertUpdateCAORAC(FormCollection form = null, string cTa = "", string cHTML = null)

My problem is that cHTML (which represents HTML of dynamically created form for printing) becomes quickly too large to be sended through query string. I would like this parameter to be sended inside request body. What I've already tried:

我的问题是cHTML(它表示动态创建的用于打印的HTML)很快变得太大,无法通过查询字符串进行发送。我希望这个参数被发送到请求体内部。我已经尝试了什么:

     $.ajax({
          url: '/SalesOfferInvoiceDeliveryNote/InsertUpdateCAORAC/',  
          type: 'POST',
          data: {
          form: $(this).parents('form').serialize(), 
          cTa: 'b',
          cHTML: cHTML
          }

In this case, parameters cTa and cHTML are sended correctly, but form element loses all it's serialized keys. (form keys with this way of ajax calling are: form, cTa and cHTML, which is completely wrong). Is there anything I could do to send all parameters inside query string body and that form doesn't loses it's keys?

在这种情况下,参数cTa和cHTML被正确地发送,但是form元素丢失了它所有的序列化键。(使用这种ajax调用方式的表单键是:form、cTa和cHTML,这是完全错误的)。是否有什么方法可以将所有参数发送到查询字符串体中,而表单不会丢失它的键?

1 个解决方案

#1


2  

Your issue is in this line:

你的问题是:

form: $(this).parents('form').serialize(), 

You can use FormData in order to create the form key/value pairs. You may take a look to Using FormData Objects.

您可以使用FormData来创建表单键/值对。您可以看看如何使用FormData对象。

In order to add the additional two parameters you can use FormData.append() like:

为了添加另外两个参数,可以使用FormData.append():

formData.append('cTa', 'b');
formData.append('cHTML', cHTML);

Your ajax can be:

ajax可以:

var formData = new FormData($(this).closest('form')[0]);
formData.append('cTa', 'b');
formData.append('cHTML', cHTML);
$.ajax({
    url: '/SalesOfferInvoiceDeliveryNote/InsertUpdateCAORAC/',
    type: 'POST',
    data: formData
});

#1


2  

Your issue is in this line:

你的问题是:

form: $(this).parents('form').serialize(), 

You can use FormData in order to create the form key/value pairs. You may take a look to Using FormData Objects.

您可以使用FormData来创建表单键/值对。您可以看看如何使用FormData对象。

In order to add the additional two parameters you can use FormData.append() like:

为了添加另外两个参数,可以使用FormData.append():

formData.append('cTa', 'b');
formData.append('cHTML', cHTML);

Your ajax can be:

ajax可以:

var formData = new FormData($(this).closest('form')[0]);
formData.append('cTa', 'b');
formData.append('cHTML', cHTML);
$.ajax({
    url: '/SalesOfferInvoiceDeliveryNote/InsertUpdateCAORAC/',
    type: 'POST',
    data: formData
});