AngularJS的POST请求的空? [英] AngularJS POSTs empty requests?
问题描述
我在AngularJS一个新手,当我试图与AngularJS一个POST请求,并帖子没有与它的参数我所面临的问题。我用西纳特拉作为一个RESTful接口。
I'm a newbie in AngularJS and I've faced an issue when I try to make a POST request with AngularJS and it POSTs no parameters with it. I use Sinatra as a RESTful interface.
这是我的西纳特拉后端的外观:
That's how my Sinatra backend looks:
post '/layer/:layer_id' do
@layer = PageLayer.where(id: params[:layer_id]).first
@layer.content = params[:content]
@layer.save
end
如果尝试用邮差Chrome扩展到邮政 - 它的作品!西纳特拉妥善保存的内容。所以我敢肯定,后端的工作,因为它应该。
If try to POST with Postman chrome extension - it works! Sinatra saves the content properly. So I'm sure that the backend works as it should.
这是我的角度测试code的外观:
That's how my angular test code looks:
TestCtrl = ($scope, $routeParams, $http, $resource) ->
$scope.layer = []
Layer = $resource('/layer/:id', {id:'@id'})
$scope.layer = Layer.get {id: $routeParams.layerId}, ->
console.log "Got you!"
$scope.saveContent = ->
$scope.layer.$save()
console.log "Saved!"
angular.module('appDirectives', []).directive "test", ->
return (scope, element, attrs) ->
element.bind "blur", ->
console.log("blur!")
scope.saveContent()
和HTML的code:
And HTML-code:
<div>Content: {{layer.content}}</div>
<div>
<form>
<input type="text" test ng-model="layer.content">
</form>
</div>
所以,唯一的问题是:怎么了?为什么我能做出正确的请求,与邮递员,但不与angularJS?角返回空的内容,所以西纳特拉将其保存为,每一次。
So, the only question is: What's wrong? Why I can make correct request with Postman but not with angularJS? Angular returns empty "content" so Sinatra saves it as "" every time.
我还附上了一层的结构:
I've also attached a structure of a layer:
g {id: 27245, page_id: 2302, external_id: 26518, original_upload: null…}
content: "dfgdfg"
external_id: 26518
id: 27245
layerNumber: 8
page_id: 2302
我怎样才能登录究竟角的帖子吗?
How can I log what exactly angular POSTs?
推荐答案
好吧,我已经解决了这个问题。不知怎的,西纳特拉没有得到正确的角职位和未自动把它们变成PARAMS。
所以,如果我们手动解析请求 - 它的作品。这样的:
All right, I've solved the issue. Somehow Sinatra was not properly getting POSTs from Angular and was not automatically putting them into params. So if we parse the request manually - it works. Like that:
post '/layer/:layer_id' do
@updated_layer = JSON.parse(request.body.string)
@layer = PageLayer.where(id: params[:layer_id]).first
@layer.content = @updated_layer['content']
@layer.save
end
这篇关于AngularJS的POST请求的空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!