在HTML表单上发送JSON数据提交

时间:2022-11-24 14:17:48

I have a html form and that form has two fields (name , description). When a user hits submit button for that form I want to submit form data in json format.

我有一个html表单,该表单有两个字段(名称,描述)。当用户点击该表单的提交按钮时,我想以json格式提交表单数据。

I tried the following:

我尝试了以下方法:

        function submitData(){
            payload.name = $("#project-name").val();
            payload.description = $("#project-description").val();

            $.post("http://localhost:5000/task-groups/add", payload);
            return false;
        }

Submitdata is called when my form's button is clicked. But this is sending form data and not json data

单击我的表单按钮时会调用Submitdata。但这是发送表单数据而不是json数据

I have a python flask server running.

我有一个python flask服务器正在运行。

[1] When I do:

[1]当我这样做时:

payload = request.get_json()
name = payload['name']

It is throwing the following exception

它抛出以下异常

File "/Users/saik/projects/mturk/server/src/index.py", line 37, in add_task_group
  name = payload['name']
TypeError: 'NoneType' object is not subscriptable

[2] However, I am able to access the data on the server side using following:

[2]但是,我可以使用以下方法访问服务器端的数据:

name = request.form['name']

Is it possible to send json data on form submit, so that I can access data using [1]

是否可以在表单提交上发送json数据,以便我可以使用[1]访问数据

The reason I am trying to send JSON data on form submit is because I have a server which serves REST API for command line clients. And I would like to use same server and endpoint to serve browser based clients.

我尝试在表单提交上发送JSON数据的原因是因为我有一个服务器为命令行客户端提供REST API。我想使用相同的服务器和端点来为基于浏览器的客户端提供服务。

4 个解决方案

#1


4  

There are 2 things you need to do to send JSON to the server with a jQuery AJAX call:

使用jQuery AJAX调用将JSON发送到服务器需要做两件事:

  • The request payload should be a string containing the JSON payload (not a JavaScript object)

    请求有效负载应该是包含JSON有效负载的字符串(不是JavaScript对象)

  • The "Content-Type" header in the HTTP request should be set to "application/json". This lets the server know that the request payload contains JSON data. $.post uses the default value of "application/x-www-form-urlencoded" for the "Content-Type" header. $.ajax allows you to set this header via the "contentType" option.

    HTTP请求中的“Content-Type”标头应设置为“application / json”。这使服务器知道请求有效负载包含JSON数据。 $ .post对“Content-Type”标头使用默认值“application / x-www-form-urlencoded”。 $ .ajax允许您通过“contentType”选项设置此标头。

Try changing your code to the following:

尝试将代码更改为以下内容:

$.ajax({
  type: 'POST',
  url: 'http://localhost:5000/task-groups/add',
  contentType: 'application/json',
  dataType: 'json',
  data: JSON.stringify(payload)
});

Note that $.post has a dataType parameter, but this controls the "Accept" request header, which is used to indicate the preferred format for the response.

请注意,$ .post具有dataType参数,但这会控制“Accept”请求标头,该标头用于指示响应的首选格式。

#2


0  

I'm not sure... but I think passing as JSON is not the right way to do this... or it's just "troublesome" to do...

我不确定......但我认为传递JSON不是正确的做法......或者它只是“麻烦”...

why not just do it like this then?!

为什么不这样做呢?!

payload = request.form
name = payload['name']

#3


0  

You need to cancel the event

您需要取消该活动

$(function() {
  $("form").on("submit",function(e) {
    e.preventDefault(); // cancel the submission
    $.post($(this).attr("action"),{ "name":$("#project-name").val(), "description":$("#project-description").val() });
  });
});

To post JSON read Post JSON to Python CGI

发布JSON读取Post JSON到Python CGI

#4


0  

using ajax call you can send successfully using this code:

使用ajax调用,您可以使用以下代码成功发送:

<script>
$(document).ready(function(){
$("#submitform").click(function(e)
{
var MyForm = JSON.stringify($("#myform").serializeJSON());
console.log(MyForm);
 $.ajax(
 {
 url : "<your url>",
 type: "POST",
 data : MyForm,

 });
 e.preventDefault(); //STOP default action

});
});
</script>

#1


4  

There are 2 things you need to do to send JSON to the server with a jQuery AJAX call:

使用jQuery AJAX调用将JSON发送到服务器需要做两件事:

  • The request payload should be a string containing the JSON payload (not a JavaScript object)

    请求有效负载应该是包含JSON有效负载的字符串(不是JavaScript对象)

  • The "Content-Type" header in the HTTP request should be set to "application/json". This lets the server know that the request payload contains JSON data. $.post uses the default value of "application/x-www-form-urlencoded" for the "Content-Type" header. $.ajax allows you to set this header via the "contentType" option.

    HTTP请求中的“Content-Type”标头应设置为“application / json”。这使服务器知道请求有效负载包含JSON数据。 $ .post对“Content-Type”标头使用默认值“application / x-www-form-urlencoded”。 $ .ajax允许您通过“contentType”选项设置此标头。

Try changing your code to the following:

尝试将代码更改为以下内容:

$.ajax({
  type: 'POST',
  url: 'http://localhost:5000/task-groups/add',
  contentType: 'application/json',
  dataType: 'json',
  data: JSON.stringify(payload)
});

Note that $.post has a dataType parameter, but this controls the "Accept" request header, which is used to indicate the preferred format for the response.

请注意,$ .post具有dataType参数,但这会控制“Accept”请求标头,该标头用于指示响应的首选格式。

#2


0  

I'm not sure... but I think passing as JSON is not the right way to do this... or it's just "troublesome" to do...

我不确定......但我认为传递JSON不是正确的做法......或者它只是“麻烦”...

why not just do it like this then?!

为什么不这样做呢?!

payload = request.form
name = payload['name']

#3


0  

You need to cancel the event

您需要取消该活动

$(function() {
  $("form").on("submit",function(e) {
    e.preventDefault(); // cancel the submission
    $.post($(this).attr("action"),{ "name":$("#project-name").val(), "description":$("#project-description").val() });
  });
});

To post JSON read Post JSON to Python CGI

发布JSON读取Post JSON到Python CGI

#4


0  

using ajax call you can send successfully using this code:

使用ajax调用,您可以使用以下代码成功发送:

<script>
$(document).ready(function(){
$("#submitform").click(function(e)
{
var MyForm = JSON.stringify($("#myform").serializeJSON());
console.log(MyForm);
 $.ajax(
 {
 url : "<your url>",
 type: "POST",
 data : MyForm,

 });
 e.preventDefault(); //STOP default action

});
});
</script>