XzAngular
XzAngular

Reputation: 199

how to change post['Content-Type'] in angularjs

i want to change post['Content-Type'] in angularjs so i use

  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';
 });

and the event is

     $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);


        });
};

however the rusult is

Parametersapplication/x-www-form-urlencoded
{"admin_name":"dd"} 

what i want is

Parametersapplication/x-www-form-urlencoded
 admin_name dd

so what i should do?

Upvotes: 17

Views: 50104

Answers (4)

xkeshav
xkeshav

Reputation: 54032

OP is using Content-Type : application/x-www-form-urlencoded so you need to use $httpParamSerializerJQLike to change post data from JSON to string

note: there is no data property but is params property

$http({
            method: 'POST',
            url: 'whatever URL',
            params:  credentials,
            paramSerializer: '$httpParamSerializerJQLike',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        })

Additionally, you can inject the serializer and use it explicitly with data property

.controller(function($http, $httpParamSerializerJQLike) {
....
$http({
        url: myUrl,
        method: 'POST',
        data: $httpParamSerializerJQLike(myData),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
     });

Upvotes: 2

aWebDeveloper
aWebDeveloper

Reputation: 38362

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';
        })

Upvotes: 6

holographic-principle
holographic-principle

Reputation: 19738

Try like:

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);
       });

Upvotes: 28

Wottensprels
Wottensprels

Reputation: 3327

Have a look at this: How can I post data as form data instead of a request payload?

Alternatively, you could do the following:

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

PHP

$post = json_decode(file_get_contents('php://input'));
$val = print_r($post->val,true);

Upvotes: -3

Related Questions