角$ http.post returnd空的错误错误、http、post、returnd

2023-09-13 03:31:11 作者:心之所向便是光

我使用的角度JS发送一些数据到服务器的NodeJS

当我使用,卷曲,我回去我发送的数据(正确的结果):

 卷曲-d{的myKey:我的价值}'-H内容类型:应用程序/ JSON'http://127.0.0.1:3000/s ?表= register_rings&安培; UID = 1'> {的myKey:我的价值} 

然而,当我使用angularjs服务,错误occures。

  .factory('RegisterRingsService',函数($ HTTP,$ Q){    //数据发送POST请求,并收到警报    发送功能(数据,UID){  $ HTTP({            方法:POST,            网址:'http://127.0.0.1:3000/s?table=register_rings&uid=1',            数据:{的myKey:我的价值}            标题:{内容类型:应用/ JSON,访问控制,允许原产地:*},            responseTyp的:JSON        })。成功(功能(数据,状态,头,配置){            警报('成功',数据,状态);        })错误(功能(数据,状态,头,配置){            警报('错误'+ JSON.stringify(数据)+ JSON.stringify(状态));        })赶上(功能(错误){            警报('抓'+ JSON.stringify(错误));        });}返回{送:送};  }) 
解决VMware Tools安装不了的问题 a913396的博客 CSDN博客 vmware无图形界面vm tools

该错误是如下:

{\"data\":null,\"status\":0,\"config\":{\"method\":\"POST\",\"transformRequest\":[null],\"transformResponse\":[null],\"url\":\"http://127.0.0.1:3000/s?table=register_rings\",\"data\":\"{\\\"MyKey\\\":\\\"My Value\\\"}\",\"headers\":{\"Content-Type\":\"application/json\",\"Access-Control-Allow-Origin\":\"*\",\"Accept\":\"application/json,纯文本/ * / *},的responseType:JSON},状态文本:}

我怀疑,我应该插入CORS头,但是我不知道该怎么做。

任何帮助将AP preciated

解决方案

问题是你如何在服务器传输数据。这是因为jQuery和角度序列化数据不同。

在默认情况下,jQuery的使用传输数据的Content-Type:X-WWW的形式urlen codeD 和熟悉的富=酒吧和放大器;巴兹=教育部序列化。 AngularJS,但是使用传输数据内容类型:应用程序/ JSON {富:酒吧,巴兹:萌 } JSON序列化,这不幸的是一些Web服务器语言 - 特别是PHP - 本身不反序列化

要解决此AngularJS开发商提供的挂钩插入 $ HTTP 服务,让我们强加 X-WWW的形式urlen codeD

  $ HTTP({   方法:POST,   网址:'...',   数据:数据,//传递数据串   标题:{内容类型:应用程序/ x-WWW的形式urlen codeD'} //设置标题等等角度传递信息的表格数据(而不是请求负载)}); 

请阅读这篇文章的一个有效的解决方案:

的http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/

I am using angular JS to send some data to nodejs server.

When I use, curl, I get back the data I send (correct result):

curl -d '{"MyKey":"My Value"}' -H "Content-Type: application/json" 'http://127.0.0.1:3000/s?table=register_rings&uid=1'
> {"MyKey":"My Value"}

However, when I use angularjs service, error occures.

.factory('RegisterRingsService', function($http, $q) {
    // send POST request with data and alert received
    function send(data, uid) {

  $http({
            method: 'POST',
            url: 'http://127.0.0.1:3000/s?table=register_rings&uid=1',
            data: '{"MyKey":"My Value"}',
            headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin":"*"},
            responseType: 'json'
        }).success(function(data, status, headers, config) {
            alert('success', data, status);
        }).error(function(data, status, headers, config) {
            alert('error' + JSON.stringify(data) + JSON.stringify(status));
        }).catch(function(error){
            alert('catch' + JSON.stringify(error));
        });
}   

return  {send : send};  
  })

The error is following:

{"data":null,"status":0,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"url":"http://127.0.0.1:3000/s?table=register_rings","data":"{\"MyKey\":\"My Value\"}","headers":{"Content-Type":"application/json","Access-Control-Allow-Origin":"*","Accept":"application/json, text/plain, */*"},"responseType":"json"},"statusText":""}

I suspect that I should insert CORS headers, but I am not sure how to do that.

Any help would be appreciated

解决方案

The problem is how you are transmitting the data over to server. This is because jQuery and Angular serialize the data differently.

By default, jQuery transmits data using 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.

To workaround this AngularJS developers provided hooks into the $http service to let us impose x-www-form-urlencoded.

$http({
   method  :'POST',
   url:'...',
   data: data, // pass in data as strings
   headers :{'Content-Type':'application/x-www-form-urlencoded'} // set the headers so angular passing info as form data (not request payload)
});

Please read this post for a working solution:

http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/