邮递员和angularjs服务中的不同反应

时间:2018-05-05 13:46:39

标签: javascript angularjs api http

所以当我从邮递员那里打电话给我时,我得到了这样的反应:

enter image description here

当我从我的angularjs服务中调用相同的请求时:

this.login = function (loginInfo) {

    return $http({
        url: 'http://localhost/igt/api/public/signin',
        headers: {
            'Content-Type': 'Application/json'
        },
        method: 'POST',
        user: {
            name: "nick",
            password: "password"
        }
    })
    .then(function(response) {
        console.log(response);
    }, function (err) {
        console.log("err:");
        console.log(err);
    });
};

我在控制台中输出如下:

enter image description here

这是:

{
  "data": null,
  "status": 400,
  "config": {
    "method": "POST",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "jsonpCallbackParam": "callback",
    "url": "http://localhost/igt/api/public/signin",
    "headers": {
      "Accept": "application/json, text/plain, */*"
    },
    "user": {
      "name": "nick",
      "password": "password"
    }
  },
  "statusText": "Bad Request",
  "xhrStatus": "complete"
}

为什么会出现这种差异?

2 个答案:

答案 0 :(得分:0)

以下是配置对象属性,通过此

设置您的http配置
method – {string} – HTTP method (e.g. 'GET', 'POST', etc)
url – {string|TrustedObject}
params – {Object.<string|Object>
data – {string|Object}
headers – {Object}
eventHandlers - {Object}
uploadEventHandlers - {Object}
xsrfHeaderName – {string}
xsrfCookieName – {string}
transformRequest – {function(data, headersGetter)|Array.<function(data, headersGetter)>} – 
transformResponse –
paramSerializer
cache 
timeout
withCredentials - {boolean}
responseType - {string}

Learn more

我认为这是问题,

user: {
         name: "nick",
         password: "password"
      }

答案 1 :(得分:0)

来自邮递员的输入应为

{
 user:{
 name:"",
password:""
}
}

但你发送的是

{
    name:"",
    password:""
    }

更改角度js代码,如

user: {
   user : {
name:"nick",
password:"password"
}
}

它会起作用。

同时将标题更改为 &#39;内容类型&#39;:&#39; application / json&#39; 从 &#39;内容类型&#39;:&#39; Application / json&#39;