$ 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); });更多推荐
发布评论