更改内容类型到"应用/ JSON" POST方法,基于REST的API [英] change Content-type to "application/json" POST method, RESTful API

查看:166
本文介绍了更改内容类型到"应用/ JSON" POST方法,基于REST的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我在AngularJS新的,我需要你的帮助。

Hi guys i'm new at AngularJS and I needed your help..

所有我只需要需要的是我的JSON POST的API,并收到适当的反应。

All I need just need is to POST my json to the API and recieve the proper response.

下面是我的JSON在哪里我不知道在哪里code这一点。

Here's my JSON where i don't know where to code this.

JSON

{ 
    "userId"      :"testAgent2",
    "token"       :"testAgent2",
    "terminalInfo":"test2",
    "forceLogin"  :"false"
}

不知道我这样做是正确的。

NOT SURE IF I'm doing this right.

CONTROLLER.JS

function UserLoginCtrl($scope, UserLoginResource) {
    //Save a new userLogin
    $scope.loginUser = function() {
        var loggedin = false;
        var uUsername = $scope.userUsername;
        var uPassword = $scope.userPassword;
        var uforcelogin = 'true';

        UserLoginResource.save();
    }
}

SERVICES.JS

angular.module('UserLoginModule', ['ngResource'])
    .factory('UserLoginResource', function($resource, $http) {
        $http.defaults.useXDomain = true;
        delete $http.defaults.headers.common['X-Requested-With'];
        $http.defaults.headers.post["Content-Type"] = "application/json"; //NOT WORKING

        return $resource('http://123.123.123.123\\:1234/SOME/LOCATION/THERE', {}, {
            save: { 
                method:'POST', 
                headers: [{'Content-Type': 'application/json'}] 
            } //NOT WORKING EITHER
        });
    });

INDEX.HTML

<html ng-app>

<head>
<script src="js/lib/angular/angular.js"></script>
<script src="js/lib/angular/angular-resource.js"></script>
</head>


<body ng-controller="UserLoginCtrl">

<form class="form-horizontal" name="form-horizontal" ng-submit="loginUser();">

<div class="button-login">

<!-- start: button-login -->
<button class="btn btn-primary" type="submit">Login</button>

</div>

</form>


</body>   


</html>

希望你能帮助我的人与这个..

Hope you can help me guys with this..

我一直让喜欢不支持的媒体类型的响应...
我不知道自己还能做些什么...

I kept on getting a response like Unsupported Media Type... I don't know what else to do...

任何建议或意见将是伟大的..谢谢!

Any suggestions or comments will be great.. THANKS !

推荐答案

发布的JSON对象是角很容易。所有你需要做的是以下内容:

Posting a JSON object is quite easy in Angular. All you need to do is the following:

创建JavaScript对象

我会从你的code使用您的确切属性。

I'll use your exact properties from your code.

var postObject = new Object();
postObject.userId = "testAgent2";
postObject.token = "testAgent2";
postObject.terminalInfo = "test2";
postObject.forceLogin = "false";

邮政对象的API

要发布一个对象的API只是你需要一个简单的$ http.post功能。见下图:

To post an object to an API you merely need a simple $http.post function. See below:

$http.post("/path/to/api/", postObject).success(function(data){
    //Callback function here.
    //"data" is the response from the server.
});

由于JSON张贴到API的默认方法,有没有需要重置。请参见上$ HTTP此快捷方式链接以获取更多信息。

至于你的code具体地说,试着改变你的保存方法,包括这个简单的POST方法。

With regards to your code specifically, try changing your save method to include this simple post method.

这篇关于更改内容类型到&QUOT;应用/ JSON&QUOT; POST方法,基于REST的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆