使用AngularJS发布JSON数据的问题

时间:2016-09-15 14:07:05

标签: angularjs post

我正面临使用Angular js发帖的特定问题,以下代码失败并显示错误:

  

预检的响应包含无效的HTTP状态代码404

如果我通过以纯文本格式传递参数来进行相同的调用,通过直接在URL中附加值来工作。感谢您的任何帮助。

例如:https://sample.com/services/srest/restserver/v1.0/authenticate/login?username=rakesh&password=somepassword

angular.module("sampleApp2",[])
.service("apiCalls",function($http) {

    var result;

    var postdata = {username: "rakesh", Password: "somepassword"} ;

    this.cobLogin = function(callback,errcallback) {

            result = $http({
            method:'POST',
            data:postdata,
            url:'https://sample.com/services/srest/restserver/v1.0/authenticate/login'

        }).then(callback, errcallback);
}
return result;
});

2 个答案:

答案 0 :(得分:0)

听起来你使用了错误的内容类型。默认值为Content-type: application/x-www-form-urlencoded。你想要Content-type: application/json。您可以在headers选项中设置它。查看$http docs

var req = {
 method: 'POST',
 url: 'http://example.com',
 headers: {
   'Content-Type': 'application/json'
 },
 data: { test: 'test' }
}

$http(req).then(function(){...}, function(){...});

答案 1 :(得分:0)

提供标题

angular.module("sampleApp2",[])
.service("apiCalls",function($http) {

    var result;

    var postdata = {username: "rakesh", Password: "somepassword"} ;

    this.cobLogin = function(callback,errcallback) {

            result = $http({
            method:'POST',
            url:'https://sample.com/services/srest/restserver/v1.0/authenticate/login',
            data:postdata,
            headers: { 'Content-Type':'application/json'}


        }).then(callback, errcallback);
}
return result;
});