0

What is the true difference between HTTP POST requests run by AngularJS and JQuery?

AngularJS:

$http.post(requestURL, {'data':myData})

JQuery:

$.post(requestURL,{data : data}

On my PHP backend, I have to run two different mechanisms to read the data. AngularJS works only with for some reason

$params = json_decode(file_get_contents('php://input'), true);
$data = $params['data'];

JQuery works with the standard

$data = $_POST['data']

How can I fix AngularJS to run on the "normal" $_POST trail?

Both fail with the opposite PHP strategy.

El Dude
  • 5,328
  • 11
  • 54
  • 101
  • Er.. AngularJS uses Web Sockets... `:)` – Praveen Kumar Purushothaman Jan 16 '16 at 23:37
  • See this: http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data – Adam Gerard Jan 16 '16 at 23:37
  • Do you need to fix it? Why not just parse the body of the request form JSON? – Stewart Jan 16 '16 at 23:41
  • 1
    @Stewart clearly because they don't both send json by default and OP is asking why – charlietfl Jan 16 '16 at 23:43
  • @charlietfl In your answer your saying that angular $http sends application/json by default. What do you mean "both don't send it by default?" – Stewart Jan 16 '16 at 23:49
  • @Stewart ok...clear it up then...they don't share same defaults...is that easier? – charlietfl Jan 16 '16 at 23:50
  • Well... no because your above comment runs contrary to the answer you have provided(which I have up voted as I believe it's true). If $http sends application/json by default that means at least one of them sends json content. Your comment says the both don't which according to your own answer is not true. The round about point I'm making is if $http sends application/json why not just parse the json from the body of the request? Surly PHP can handle a non form encoded post. – Stewart Jan 16 '16 at 23:54
  • @Stewart I think you are missing the point that requests could come from either library/framework...which means normalizing somewhere – charlietfl Jan 17 '16 at 00:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100870/discussion-between-stewart-and-charlietfl). – Stewart Jan 17 '16 at 00:03
  • Let's get ready to rumble! :) I appreciate the effort of you two guys, thank you for your efforts. Good day! – El Dude Jan 17 '16 at 00:08

1 Answers1

3

The difference is default content types are different.

jQuery $.ajax uses application/x-www-form-urlencoded

Angular $http uses application/json

Both API's provide options to switch content type .

$http also has serialization service $httpParamSerializerJQLike to form encode and the opposite would be use JSON.stringify() for `$.ajax

charlietfl
  • 170,828
  • 13
  • 121
  • 150