Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将ajax中的FormData和JSON对象传递给SpringMVC控制器?_Json_Ajax_Spring Mvc_Form Data - Fatal编程技术网

如何将ajax中的FormData和JSON对象传递给SpringMVC控制器?

如何将ajax中的FormData和JSON对象传递给SpringMVC控制器?,json,ajax,spring-mvc,form-data,Json,Ajax,Spring Mvc,Form Data,我需要在ajax调用中同时传递FormData和JSON对象,但是我得到了一个400错误的请求错误 [artifact:mvn] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not

我需要在ajax调用中同时传递FormData和JSON对象,但是我得到了一个400错误的请求错误

[artifact:mvn]  org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
[artifact:mvn]  at [Source: java.io.PushbackInputStream@3c0d58f6; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
[artifact:mvn]  at [Source: java.io.PushbackInputStream@3c0d58f6; line: 1, column: 3]
JS:

控制器:

@RequestMapping(value="/processSaveItem",method = RequestMethod.POST")
public @ResponseBody Map<String,String> processSaveItem(
                                  @RequestBody XYZClass result[])

}
@RequestMapping(value=“/processSaveItem”,method=RequestMethod.POST”)
public@ResponseBody映射进程saveItem(
@RequestBody XYZClass结果[])
}
有一个类似的问题,我也在用同样的方法尝试


如何在一个ajax请求中同时发送FormData和JSON对象?

您可以这样传递

   postdata={};
   postdata.formData=formData;
   postData.jsonData=jsonData
 $.ajax({
    type: "POST",
    url: postDataUrl,
    data: { postdata},
    processData: false,
    contentType: false,
    async: false,
    cache: false,
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    success: function(data, textStatus, jqXHR) {
    }
}))


在控制器端,您可以识别数据。

您可以通过这种方式传递数据

   postdata={};
   postdata.formData=formData;
   postData.jsonData=jsonData
 $.ajax({
    type: "POST",
    url: postDataUrl,
    data: { postdata},
    processData: false,
    contentType: false,
    async: false,
    cache: false,
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    success: function(data, textStatus, jqXHR) {
    }
}))


在控制器端,您可以识别数据。

我通过在ajax POST中添加带有FormData对象的JSON字符串解决了这个问题

var jsonObjectData = JSON.stringify(jcArray);
formData.append("jsonObjectData",jsonObjectData);
在控制器中,您可以像访问其他表单数据值一样以正常方式访问它

request.getParameter("jsonObjectData");
现在您将拥有字符串化的JSON,您可以将JSON解析为Java对象


.

我通过在ajax POST中添加带有FormData对象的JSON字符串解决了这个问题

var jsonObjectData = JSON.stringify(jcArray);
formData.append("jsonObjectData",jsonObjectData);
在控制器中,您可以像访问其他表单数据值一样以正常方式访问它

request.getParameter("jsonObjectData");
现在您将拥有字符串化的JSON,您可以将JSON解析为Java对象


.

您可以执行以下操作以在Ajax调用中传递表单数据

var formData = $('#client-form').serialize();
$.ajax({
    url: 'www.xyz.com/index.php?' + formData,
    type: 'POST',
    data:{
    },
    success: function(data){},
    error: function(data){},
})

可以执行以下操作以在Ajax调用中传递表单数据

var formData = $('#client-form').serialize();
$.ajax({
    url: 'www.xyz.com/index.php?' + formData,
    type: 'POST',
    data:{
    },
    success: function(data){},
    error: function(data){},
})

lol。这不是我想要的。这如何回答我的问题?答案中的json数据在哪里?在数据部分,您可以传递带有引用的json对象,表单数据被附加到URL部分。例如,数据:{jsonObject:jsonObject},您没有正确理解我的问题。我需要同时发送json对象和formData。您的回答意味着您只发送了其中一个。这里我也发送了我的表单数据。我正在Url.lol中传递我的表单数据。这不是我想要的。这如何回答我的问题以及json数据在您的答案中的位置?在data sect中您可以传递带有引用的json对象,并且表单数据被追加到URL部分中。例如,数据:{jsonObject:jsonObject},您没有正确理解我的问题。我需要同时发送json对象和formData。您的回答意味着您只发送其中一个。这里我也发送表单数据。我在Url中传递表单数据。