将内容类型更改为“应用程序/json";POST 方法,RESTful API [英] change Content-type to "application/json" POST method, RESTful API

查看:17
本文介绍了将内容类型更改为“应用程序/json";POST 方法,RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 AngularJS 的新手,我需要你的帮助.

我只需要将我的 json 发布到 API 并收到正确的响应.

这是我的 JSON,我不知道在哪里编码.

JSON

<代码>{用户 ID":testAgent2",令牌":testAgent2",终端信息":test2",强制登录":假"}

不确定我这样做是否正确.

CONTROLLER.JS

function UserLoginCtrl($scope, UserLoginResource) {//保存一个新用户登录$scope.loginUser = function() {变量登录 = 假;var uUsername = $scope.userUsername;var uPassword = $scope.userPassword;var uforcelogin = '真';UserLoginResource.save();}}

SERVICES.JS

angular.module('UserLoginModule', ['ngResource']).factory('UserLoginResource', function($resource, $http) {$http.defaults.useXDomain = true;删除 $http.defaults.headers.common['X-Requested-With'];$http.defaults.headers.post["Content-Type"] = "application/json";//不工作返回 $resource('http://123.123.123.123\\:1234/SOME/LOCATION/THERE', {}, {节省: {方法:'POST',标头:[{'Content-Type':'application/json'}]}//不工作});});

INDEX.HTML

<头><script src=js/lib/angular/angular.js"></script><script src=js/lib/angular/angular-resource.js"></script><body ng-controller="UserLoginCtrl"><form class="form-horizo​​ntal";名称=形式-水平";ng-submit="loginUser();"><div class="button-login"><!-- 开始:按钮登录--><button class="btn btn-primary";type=提交">登录</按钮>

</表单></html>

我不断收到诸如不支持的媒体类型"之类的回复.我不知道,还能做什么.

解决方案

在 Angular 中发布 JSON 对象非常容易.您需要做的就是:

创建 Javascript 对象

我将使用您代码中的确切属性.

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

将对象发布到 API

要将对象发布到 API,您只需要一个简单的 $http.post 函数.见下文:

$http.post("/path/to/api/", postObject).success(function(data){//这里的回调函数.//数据"是来自服务器的响应.});

由于 JSON 是发布到 API 的默认方法,因此无需重新设置.有关详细信息,请参阅有关 $http 快捷方式的此链接.

特别是关于您的代码,请尝试更改您的保存方法以包含这个简单的 post 方法.

I am new at AngularJS and I needed your help.

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

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>

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

解决方案

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

Create a Javascript Object

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";

Post the object to the API

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.
});

Since JSON is the default method of posting to an API, there's no need to reset that. See this link on $http shortcuts for more information.

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

这篇关于将内容类型更改为“应用程序/json";POST 方法,RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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