如何在angularjs中更改post[';内容类型';]

如何在angularjs中更改post[';内容类型';],angularjs,Angularjs,我想更改angularjs中的post['Content-Type'],以便使用 app.config(function($locationProvider,$httpProvider) { $locationProvider.html5Mode(false); $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; $httpPro

我想更改angularjs中的post['Content-Type'],以便使用

  app.config(function($locationProvider,$httpProvider) {
$locationProvider.html5Mode(false);
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;        charset=UTF-8';
 });
这件事是

     $http.post("http://172.22.71.107:8888/ajax/login",{admin_name:user.u_name,admin_password:user.cert})
        .success(function(arg_result){

            console.log(arg_result);


        });
};
然而结果是

Parametersapplication/x-www-form-urlencoded
{"admin_name":"dd"} 
我想要的是

Parametersapplication/x-www-form-urlencoded
 admin_name dd
那我该怎么办呢?

看看这个:

或者,您可以执行以下操作:

$http.post('file.php',{
        'val': val
    }).success(function(data){
            console.log(data);
        });
PHP

试试看:

var serializedData = $.param({admin_name:user.u_name,admin_password:user.cert});

$http({
    method: 'POST',
    url: 'http://172.22.71.107:8888/ajax/login',
    data: serializedData,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }}).then(function(result) {
           console.log(result);
       }, function(error) {
           console.log(error);
       });

OP正在使用
内容类型:application/x-www-form-urlencoded
,因此您需要使用将post数据从JSON更改为字符串

注意:没有数据属性,但有params属性

此外,您可以插入序列化程序,并将其显式地与data属性一起使用


非常感谢你!它工作得很好!但是我不知道“$.param”的函数是什么。param是一个jQuery函数,用于将表单数据序列化为字符串。使用
application/x-www-form-urlencoded
@XzAngular时,需要像这样序列化JSON,您可以使用JSON.stringify(requestObject)而不是$.param。它不需要jQuery。非常好!我在飞行前的选项请求中遇到了问题,并没有使用合适的标题进行响应,但post确实如此。使用上面的代码并将其从“application/x-www-form-urlencoded”更改为“text/plain”,为我解决了这个问题。默认内容类型为“application/json”,是的。以fiddle或plunkr的形式提供您的问题,我会看一看这是一个不完整的解决方案,如果您想指定除默认内容类型之外的其他内容类型,则必须在http服务中声明它。我并不是说你的解决方案不起作用,或者它确实起作用,但这并不是我要问的问题。我花了大约两个小时的时间在这个问题上。谢谢
var serializedData = $.param({admin_name:user.u_name,admin_password:user.cert});

$http({
    method: 'POST',
    url: 'http://172.22.71.107:8888/ajax/login',
    data: serializedData,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }}).then(function(result) {
           console.log(result);
       }, function(error) {
           console.log(error);
       });
angular.module('myApp', [])
        .config(function ($httpProvider) {
            $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
            $httpProvider.defaults.headers.post['Content-Type'] =  'application/x-www-form-urlencoded';
        })
$http({
            method: 'POST',
            url: 'whatever URL',
            params:  credentials,
            paramSerializer: '$httpParamSerializerJQLike',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        })
.controller(function($http, $httpParamSerializerJQLike) {
....
$http({
        url: myUrl,
        method: 'POST',
        data: $httpParamSerializerJQLike(myData),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
     });