$ http.post返回带有角度和粗线的422($http.post return 422 with angular and grails)

编程入门 行业动态 更新时间:2024-10-25 16:29:12
$ http.post返回带有角度和粗线的422($http.post return 422 with angular and grails)

我使用grails 3.x作为宁静的服务提供商。 提供简单的休息资源,如:

@Resource(uri ='/users', readOnly = false, formats = ['json', 'xml']) class User { String username String email String password }

我已成功通过curl测试它,如:

$ curl -H "Content-Type: application/json" -X POST -d "{\"username\":\"xyz\",\"password\":\"xyz\", \"email\":\"w@g.com\"}" http://localhost:8080/users {"id":1,"email":"w@g.com","password":"xyz","username":"xyz"}

但是当我使用angularjs来执行http post ajax调用时,

$http.post(url, {"username":"dummy", "password":"test", "email":"email@g.com"}) .success(function(data, status, headers, config) { // $location.path('/listUsers'); console.log("data " + data); }).error(function(data, status, headers, config) { console.log("error occured... " + status); });

它总是抛出422状态,即使具有相同的确切值。

I am using grails 3.x as the restful service provider. A simple rest resource is provided like:

@Resource(uri ='/users', readOnly = false, formats = ['json', 'xml']) class User { String username String email String password }

I've tested it by curl successfully like :

$ curl -H "Content-Type: application/json" -X POST -d "{\"username\":\"xyz\",\"password\":\"xyz\", \"email\":\"w@g.com\"}" http://localhost:8080/users {"id":1,"email":"w@g.com","password":"xyz","username":"xyz"}

but when I use angularjs to perform the http post ajax call,

$http.post(url, {"username":"dummy", "password":"test", "email":"email@g.com"}) .success(function(data, status, headers, config) { // $location.path('/listUsers'); console.log("data " + data); }).error(function(data, status, headers, config) { console.log("error occured... " + status); });

it always throw the 422 status even with the same exact values.

最满意答案

您可以将此标题全局放置如下 -

angular.module('myApp', []) .config(function ($httpProvider) { $httpProvider.defaults.headers.put['Content-Type'] = 'application/json'; $httpProvider.defaults.headers.post['Content-Type'] = 'application/json'; })

Finally I figured it out. Thank you for all your comments.

var config = { 'method': 'POST', 'url': "http://localhost:8080/users", 'headers': { 'Content-Type': 'application/json' }, 'data': {"username":"dummy", "password":"test", "email":"email@g.com"} } $http(config) .success(function(data, status, headers, config) { // $location.path('/listUsers'); console.log("data " + data); }).error(function(data, status, headers, config) { console.log("error occured... " + status); });

更多推荐

本文发布于:2023-07-23 18:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235722.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:角度   粗线   post   http   grails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!