17

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?

XzAngular
  • 199
  • 1
  • 2
  • 6

4 Answers4

28

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);
       });
holographic-principle
  • 19,688
  • 10
  • 46
  • 62
  • thank you very much !it works well!but i do not not the function of "$.param" – XzAngular Jul 12 '13 at 08:46
  • `$.param` is a jQuery function for serializing form data into a string. http://api.jquery.com/jQuery.param/ . It's required to serialize your JSON like this when using `application/x-www-form-urlencoded` – holographic-principle Jul 12 '13 at 08:55
  • 1
    @XzAngular, you can use *JSON.stringify(requestObject)* instead of *$.param*. It does not require jQuery. – pubsy Mar 24 '15 at 05:47
  • Very nice! I had problems with the preflight options request didn't respond with the appropriate headers but post did. Using the code above and changing it from "application/x-www-form-urlencoded" to "text/plain" fixed this for me. Default the content type would be "application/json" – Arne H. Bitubekk Apr 22 '15 at 08:12
6
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';
        })
aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
2

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'
        }
     });
xkeshav
  • 53,360
  • 44
  • 177
  • 245
-3

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);
Community
  • 1
  • 1
Wottensprels
  • 3,307
  • 2
  • 29
  • 38
  • Yes it does. Provide your issue as a fiddle or plunkr and I will have a look – Wottensprels Nov 18 '14 at 09:31
  • This is an incomplete solution, if you want to specify a different content type besides the default, you must declare it inside the http service. I am not saying your solution does not work or it does work, but that is not what was asked. – TGarrett Jan 20 '17 at 21:50