标签:
$http
service shorthand functions ($http.post()
, etc.) don’t appear to be swappable with the jQuery equivalents (jQuery.post()
, etc.) even though the respective manuals imply identical usage. That is, if your jQuery code looked like this before:
{ foo: ‘bar‘ }
params from the AngularJS request.Content-Type: x-www-form-urlencoded
and the familiar foo=bar&baz=moe
serialization. AngularJS, however, transmits data using Content-Type: application/json
and { "foo": "bar", "baz": "moe" }
JSON serialization, which unfortunately some Web server languages—notably PHP—do not unserialize natively.Content-Type: x-www-form-urlencoded
将data转码为foo=bar&baz=moe
的形式。ng则是Content-Type: application/json
来发送{ "foo": "bar", "baz": "moe" }
的JSON串,这使得服务器端(尤其是PHP)无法解读这样的数据。$http
service to let us impose x-www-form-urlencoded
on all our transmissions. There are many solutions people have offered thus far on forums and StackOverflow, but they are not ideal because they require you to modify either your server code or your desired usage pattern of $http
. Thus, I present to you the best possible solution, which requires you to change neither server nor client code but rather make some minor adjustments to $http
‘s behavior in the config of your app’s AngularJS module:x-www-form-urlencoded
发送数据,关于这点很多人提供了解决办法,但都不太理想,因为你不得不去修改你的服务器代码或者$http的代码。介于此,我给出了可能是最优的解决方案,前后端的代码均无需修改:jQuery.param()
in place of the above homegrown param()
function; it will cause havoc when you try to use AngularJS $resource
because jQuery.param()
will fire every method on the $resource
class passed to it! (This is a feature of jQuery whereby function members of the object to parametrize are called and the return values are used as the parametrized values, but for our typical use case in AngularJS it is detrimental since we typically pass “real” object instances with methods, etc.)$http
service shorthand functions ($http.post()
, etc.) don’t appear to be swappable with the jQuery equivalents (jQuery.post()
, etc.) even though the respective manuals imply identical usage. That is, if your jQuery code looked like this before:{ foo: ‘bar‘ }
params from the AngularJS request.Content-Type: x-www-form-urlencoded
and the familiar foo=bar&baz=moe
serialization. AngularJS, however, transmits data using Content-Type: application/json
and { "foo": "bar", "baz": "moe" }
JSON serialization, which unfortunately some Web server languages—notably PHP—do not unserialize natively.Content-Type: x-www-form-urlencoded
将data转码为foo=bar&baz=moe
的形式。ng则是Content-Type: application/json
来发送{ "foo": "bar", "baz": "moe" }
的JSON串,这使得服务器端(尤其是PHP)无法解读这样的数据。$http
service to let us impose x-www-form-urlencoded
on all our transmissions. There are many solutions people have offered thus far on forums and StackOverflow, but they are not ideal because they require you to modify either your server code or your desired usage pattern of $http
. Thus, I present to you the best possible solution, which requires you to change neither server nor client code but rather make some minor adjustments to $http
‘s behavior in the config of your app’s AngularJS module:x-www-form-urlencoded
发送数据,关于这点很多人提供了解决办法,但都不太理想,因为你不得不去修改你的服务器代码或者$http的代码。介于此,我给出了可能是最优的解决方案,前后端的代码均无需修改:jQuery.param()
in place of the above homegrown param()
function; it will cause havoc when you try to use AngularJS $resource
because jQuery.param()
will fire every method on the $resource
class passed to it! (This is a feature of jQuery whereby function members of the object to parametrize are called and the return values are used as the parametrized values, but for our typical use case in AngularJS it is detrimental since we typically pass “real” object instances with methods, etc.)翻译-让ng的$http服务与jQuerr.ajax()一样易用
标签:
原文地址:http://www.cnblogs.com/xd1024/p/5906681.html