gpt4 book ai didi

javascript - Backbone 帖子 JSON 为空?

转载 作者:行者123 更新时间:2023-12-02 17:15:37 24 4
gpt4 key购买 nike

我正在尝试在 Rails Survey 应用程序上创建一个基本 Backbone ,以了解嵌套模型并与服务器交互。我正在努力在用户选择后实际创建选择。

这是一个curl请求,从JSON/POST的 Angular 来看,它应该是有效的/理想的:

curl -XPOST -H "Content-Type: application/json" http://localhost:3000/choices -d '{"answer_id":"3", "appuser_id":"1", "question_id":"2"}'

这是我的创建方法:

updateQuestion: ->
if @questionNumber < @questionLimit
@questionNumber += 1
$("#container").html(@render().el)
choice = new SurveyMe.Models.Choice
choice.appuser = Cookie.get('user')
choice.question = 3
choice.answer = 4
choice.save()
else
Backbone.history.navigate("surveys",trigger: true)

选择模型:

class SurveyMe.Models.Choice extends Backbone.RelationalModel

urlRoot: '/choices'

认为 JSON 可能会显示为 null?知道为什么会这样或者我需要做什么来在curl中创建上面的JSON吗?

标题:

Remote Address:127.0.0.1:3000
Request URL:http://localhost:3000/choices
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:2
Content-Type:application/json
Cookie:request_method=GET; user=6c7f11ad2bd9d825cc89f77b8f24d008; _survey_me_session=ZTh0MHVsUTd0amx6YnpPMEs3NlE4OWtkM0dDdjEzNndzUFc0OFdMS1NSWFZibHJFRXpDdGJRdVZETW9qSUQ3dUNVZjF1Q2QrdndKWVBWN1Z0M2VwRnZRSU95a3ZrU1JPSjArMjgxdmFMT3FXSk43L21kOTQxdVJJejZjb1p5V21EdUIwenlKbjc1dFlCbUtXd3B6TTNKRjhqSGxvd0FobC9Nblo4Y0xRL0VXMjhqb2xrb3B1Ryt3UnRERUFDRGhnLS1NWGc4eGlKSzRTZm96azJqL0ZhbjhRPT0%3D--f00afae157b22e2f4f5a052e415603070b476f2e
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/surveys/1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
X-CSRF-Token:yJTVE9Ce+a6uIfHk7QmXZsRtg4i7kAoFKjGzXs1Ileg=
X-Requested-With:XMLHttpRequest
Request Payloadview source
{}
No Properties
Response Headersview source
Connection:Keep-Alive
Content-Length:16491
Content-Type:text/html; charset=utf-8
Date:Sun, 29 Jun 2014 03:22:21 GMT
Server:WEBrick/1.3.1 (Ruby/2.0.0/2013-05-14)
X-Request-Id:4276e2be-224c-4829-87ce-0a40e960d5dd
X-Runtime:0.105006

预览:

ActionController::ParameterMissing in ChoicesController#create
param not found: choice

Extracted source (around line #48):
4647484950
# Never trust parameters from the scary internet, only allow the white list through.
def choice_params
params.require(:choice).permit(:appuser_id, :answer_id, :question_id)
end
end
Rails.root: C:/Users/thammond/Documents/GitHub/Survey.me

Application Trace | Framework Trace | Full Trace
app/controllers/choices_controller.rb:48:in `choice_params'app/controllers/choices_controller.rb:12:in `create'
Request

Parameters:

{"choice"=>{}}
Toggle session dump
Toggle env dump
Response

Headers:

None

最佳答案

模型上的主干模型属性和 JavaScript 对象属性是完全不同的东西。当你这样说时:

choice.appuser = Cookie.get('user')

您正在设置 appuser 属性,而不是 appuser 属性。当您对主干模型执行 CRUD 操作时,它们才知道属性。您想使用set设置属性以便 save 了解它们:

choice = new SurveyMe.Models.Choice
choice.set('appuser', Cookie.get('user'))
choice.set('question', 3)
choice.set('answer, 4)
choice.save()

或者一次性设置它们:

choice = new SurveyMe.Models.Choice
choice.set(
appuser: Cookie.get('user')
question: 3
answer: 4
)
choice.save()

或者甚至将它们交给save

choice = new SurveyMe.Models.Choice
choice.save(
appuser: Cookie.get('user')
question: 3
answer: 4
)

关于javascript - Backbone 帖子 JSON 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24472923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com