Post not working as expected in angularjs

☆樱花仙子☆ 提交于 2019-12-30 10:06:54

问题


I am using an external api, which works very good in postman but not working when i call from angularjs.

Here is how i call from my angular js

$http.post('http://api.quickblox.com/users.json', {
    token: '2ba123a8c43664886c66702fb81b779b094cc7b8',
    'user[email]': email,
    'user[login]': email,
    'user[login]': email,
    'user[password]': password
}).then(function (results) {
    console.log('mid');
});

Here is preview of the image

It works good.

But it is not working when i do from angularjs call

Here is screenshot the response when i do angularjs call


回答1:


Looks like you need to provide additional "Content-Type" header and reformat the data you are sending:

$http.post('http://api.quickblox.com/users.json', {
    token: '2ba123a8c43664886c66702fb81b779b094cc7b8',
    user: {
        email: email,
        login: email,
        password: password
    }
}, {
    'Content-Type': 'application/x-www-form-urlencoded'
})
.then(function(results) {
    console.log('mid');
})
.catch(function(response) {
    console.log('Error', response.status, response.data.errors);
});

Demo: http://plnkr.co/edit/ishIqko1GHT7IGvXV8ZF?p=preview



来源:https://stackoverflow.com/questions/31218982/post-not-working-as-expected-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!