douyiken0968 2016-01-16 23:30
浏览 78
已采纳

HTTP post请求 - AngularJS和JQuery之间的区别

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.

  • 写回答

1条回答 默认 最新

  • dongqiao9394 2016-01-16 23:39
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?