Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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 如何将$http请求转换为$resource请求_Javascript_Angularjs_Rest - Fatal编程技术网

Javascript 如何将$http请求转换为$resource请求

Javascript 如何将$http请求转换为$resource请求,javascript,angularjs,rest,Javascript,Angularjs,Rest,我有一个$http POST请求可以工作 $http.post('api/saveFile', formData, { transformRequest: angular.identity, headers : { 'Content-Type' : undefined } } 同时,当我使用$resource时,我得到一个500错误,基本上我的后端从前端看到一个空值 这是资源的代码 this

我有一个$http POST请求可以工作

$http.post('api/saveFile', formData, {

           transformRequest: angular.identity,

           headers : {
            'Content-Type' : undefined
           }
}
同时,当我使用$resource时,我得到一个500错误,基本上我的后端从前端看到一个空值

这是资源的代码

     this.resource = $resource('api/myUrl', {}, {

        'saveFile': {
            method: 'POST',
            url: 'api/myUrl/saveFile',
            data: {file : '@file'},
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        }
    });
这里是调用资源方法的地方。注意:我对它进行了测试,因此请求前后的代码是相同的

MyService.resource.saveFile(
            {file : formData},
                function successCallback(response) {
                    console.log(response);
                },
                function errorCallback(response) {
                    console.log(response);
                })

因此,这里的代码假设将一个文件上载到后端,然后将其保存到驱动器。这对$http有效,但对$resource无效。

有帮助吗?@Pete,不,没有,它给了我更多关于如何格式化文件上传的信息,但没有我的问题。我试着使用一个答案中演示的类似语法,但没有帮助。问题是我不明白应该如何准确地发送数据。有帮助吗?@Pete,没有,它给了我更多关于如何格式化文件上传的信息,但没有我的问题。我试着使用一个答案中演示的类似语法,但没有帮助。问题是我不明白应该如何准确地发送数据。