我可以在不使用AJAX的情况下发布JSON吗?

时间:2022-10-07 17:24:18

I have some data, lets say:

我有一些数据,让我们说:

var dat = JSON.stringify(frm.serializeArray())

I want to submit this to the server using a roundtrip (aka, non ajax).

我想使用往返(aka,non ajax)将其提交给服务器。

I know this is possible, but I can't find any literature on it. Ideas?

我知道这是可能的,但我找不到任何关于它的文献。想法?

(I am using jQuery, if that makes it easier)

(我正在使用jQuery,如果这更容易)

EDIT: while all of these answers so far answer the question, I should have included that I want an "content type" of "application/json"

编辑:虽然所有这些答案到目前为止回答了这个问题,但我应该包括我想要一个“application / json”的“内容类型”

4 个解决方案

#1


7  

  1. Create an HTML form with unique "id" attribute. You can hide it using CSS "display:none". Also fill the action and method attributes.
  2. 创建具有唯一“id”属性的HTML表单。您可以使用CSS“display:none”隐藏它。还要填写操作和方法属性。
  3. Add a text or hidden input field to the form. make sure you give it a meaningful "name" attribute. That's the name that the server would get the data within.
  4. 向表单添加文本或隐藏的输入字段。确保给它一个有意义的“名称”属性。这是服务器获取数据的名称。
  5. Using JQuery (or plain old javascript) copy the variable "dat" into the input field
  6. 使用JQuery(或普通的旧javascript)将变量“dat”复制到输入字段中
  7. Submit the form using script
  8. 使用脚本提交表单

#2


4  

There is a working draft to support the so called HTML-JSON-FORMS, see: http://www.w3.org/TR/2014/WD-html-json-forms-20140529/

有一个支持所谓的HTML-JSON-FORMS的工作草案,请参阅:http://www.w3.org/TR/2014/WD-html-json-forms-20140529/

So far use ajax or send the json into an input text field.

到目前为止使用ajax或将json发送到输入文本字段。

#3


2  

You would need to assign the json string to an input's value inside a form tag in order for it to get POSTed to the server (either by the user submitting the form or by clicking the submit button programmatically).

您需要将json字符串分配给表单标记内的输入值,以便将其发送到服务器(通过用户提交表单或通过编程方式单击提交按钮)。

Alternatively from javascript you could use window.location to send the variable as part of a GET request.

或者从javascript中,您可以使用window.location将变量作为GET请求的一部分发送。

#4


2  

<form action="xxx.aspx" method="POST">
  <input type='hidden' id='dat' />

  <!-- Other elements -->
</form>

<script type='text/javascript'>
  $('#dat').val(JSON.stringify(frm.serializeArray()));
</script>

#1


7  

  1. Create an HTML form with unique "id" attribute. You can hide it using CSS "display:none". Also fill the action and method attributes.
  2. 创建具有唯一“id”属性的HTML表单。您可以使用CSS“display:none”隐藏它。还要填写操作和方法属性。
  3. Add a text or hidden input field to the form. make sure you give it a meaningful "name" attribute. That's the name that the server would get the data within.
  4. 向表单添加文本或隐藏的输入字段。确保给它一个有意义的“名称”属性。这是服务器获取数据的名称。
  5. Using JQuery (or plain old javascript) copy the variable "dat" into the input field
  6. 使用JQuery(或普通的旧javascript)将变量“dat”复制到输入字段中
  7. Submit the form using script
  8. 使用脚本提交表单

#2


4  

There is a working draft to support the so called HTML-JSON-FORMS, see: http://www.w3.org/TR/2014/WD-html-json-forms-20140529/

有一个支持所谓的HTML-JSON-FORMS的工作草案,请参阅:http://www.w3.org/TR/2014/WD-html-json-forms-20140529/

So far use ajax or send the json into an input text field.

到目前为止使用ajax或将json发送到输入文本字段。

#3


2  

You would need to assign the json string to an input's value inside a form tag in order for it to get POSTed to the server (either by the user submitting the form or by clicking the submit button programmatically).

您需要将json字符串分配给表单标记内的输入值,以便将其发送到服务器(通过用户提交表单或通过编程方式单击提交按钮)。

Alternatively from javascript you could use window.location to send the variable as part of a GET request.

或者从javascript中,您可以使用window.location将变量作为GET请求的一部分发送。

#4


2  

<form action="xxx.aspx" method="POST">
  <input type='hidden' id='dat' />

  <!-- Other elements -->
</form>

<script type='text/javascript'>
  $('#dat').val(JSON.stringify(frm.serializeArray()));
</script>