Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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
如何在AngularJS中的$http请求中指定数据类型和内容类型_Angularjs_Request - Fatal编程技术网

如何在AngularJS中的$http请求中指定数据类型和内容类型

如何在AngularJS中的$http请求中指定数据类型和内容类型,angularjs,request,Angularjs,Request,我在互联网上找不到如何在AngularJS中将数据类型和内容类型传递给快捷方式请求 例如: $http.get('url',{ headers:{'header1':'value'} }) 第二个问题:我可以用这种代码发送标题吗 beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Bearer ' + Api.getToken());

我在互联网上找不到如何在AngularJS中将数据类型和内容类型传递给快捷方式请求

例如:

$http.get('url',{
                 headers:{'header1':'value'}
})
第二个问题:我可以用这种代码发送标题吗

beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', 'Bearer ' + Api.getToken());
            }

您可以像这样添加内容类型,但为此,您还需要在请求中指定数据

 return $http({
               method: 'POST',
               //withCredentials:true,
               headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
               data: data,
               url: yourUrlHere
             });
数据可以是空字符串,但如果不添加数据,则不会设置内容类型

简而言之,你可以这样做

 $http.post('/someUrl', data, {headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}}).then(successCallback, errorCallback);

是的,但是我怎么能用快捷方式呢?像$http.getget方法不需要内容类型,因为您不会使用getSorry提交任何数据,然后使用$http.post:Di更新了您需要在post函数的第三个参数中添加选项的答案