Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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
Javascript Json对象解析并传递给spring控件_Javascript_Json_Ajax_Spring - Fatal编程技术网

Javascript Json对象解析并传递给spring控件

Javascript Json对象解析并传递给spring控件,javascript,json,ajax,spring,Javascript,Json,Ajax,Spring,我的jsp中有一个文件上传组件,我只接受json格式的文件。我想获取json文件并将其传递给spring,然后在spring中对json对象进行操作 我的javascript如下所示 同样,当我上传文件时,它会命中控制器类,但在浏览器控制台中,它会抛出错误 “网络错误:404未找到-http://localhost:8080/proj/fileUpload" 但是当我试图从JSON.stringify(硬编码) Json文件内容: 在javascript中尝试url:fileUpload 您的页

我的jsp中有一个文件上传组件,我只接受json格式的文件。我想获取json文件并将其传递给spring,然后在spring中对json对象进行操作

我的javascript如下所示 同样,当我上传文件时,它会命中控制器类,但在浏览器控制台中,它会抛出错误

“网络错误:404未找到-http://localhost:8080/proj/fileUpload"

但是当我试图从
JSON.stringify(硬编码)

Json文件内容: 在javascript中尝试
url:fileUpload


您的页面未命中控制器,这就是它给出404错误的原因

您如何知道它命中了服务器?你所说的打印编码值是什么意思?在服务器或客户机上?它击中了正在打印的服务器sysout。打印硬编码的值是,而不是上传文件,将json放入变量并在浏览器控制台中打印。这可能有助于控制器正常运行。。我正在通过debug和SysOut检查您现在遇到的问题。?
var file = this.files[0];
 var reader = new FileReader();

  var oMyForm = new FormData();
  oMyForm.append("file", this.files[0]);

 console.log('reader='+JSON.stringify(reader)) // this does not print any it prints empty as "reader={}"
console.log('oMyForm ='+JSON.stringify(oMyForm )) // this does not print any it prints empty as "oMyForm={}"
var urlpath =  "<%=request.getContextPath()%>/fileUpload";
  $.ajax({
                    data : JSON.stringify(reader),
                    type : 'POST',
                    dataType: 'json',
                    headers: { 
                        'Accept': 'application/json',
                        'Content-Type': 'application/json' 
                    },
                    url : urlpath,
                    success : function(data){
                    }
               });
@RequestMapping(value= "/fileUpload", method=RequestMethod.POST,consumes="application/json")
    public void fileUpload(){
         //here i want to get the json object and do parsing and manipulations on the object
         System.out.println(" uploaded!");
    }
{'Gyr-x':10.11,'Gyr-y':9.66,'Gyr-z':10.93,'Temparature':30,'Pressure':101,'Humidity':15,'deviceId':1}
@RequestMapping(value= "/fileUpload", method=RequestMethod.POST,consumes="application/json")
public void fileUpload(@requestBody String json){
     //here i want to get the json object and do parsing and manipulations on the object
     System.out.println(" uploaded!");
}