我正在序列化一个表单,并成功获取我想要发送的对象,但是当发出POST请求时,浏览器只传递playerName
属性,只有该属性,即使我更改了输入名称并传递了另一个属性playerName
它不会通过,除非它是playerName
attr。序列化一个对象然后发送到服务器,laravel?
这里是我的backbone.js javascript代码来保存我的表单作为一个对象。
savePlayer: function(e) {
e.preventDefault();
var playerDetails = $(e.currentTarget).serializeObject();
console.log(playerDetails); // this returns perfectly with all attributes
// I tried this with a model, saving it
var player = new App.Models.Player();
player.save(playerDetails);
// and with a collection creating it
var playersCollection = new App.Collections.PlayersCollection();
playersCollection.create(playerDetails);
return false;
}
现在我想救两个属性playerName
和teamID
,这样的形式连载适当喜欢这个{playerName: 'mike', teamID: '2'}
时保存/创建被称为是有POST方法和处理是下面的控制器。
// First attempt
public function store()
{
return Player::create(array(
'playerName' => Input::get('playerName'),
'teamID' => Input::get('teamID')
));
}
//Second try
public function store()
{
// I used this
$input = Input::json();
// And this (not same time lol)
$input = Input::all();
return Player::create(array(
'playerName' => $input->playerName,
'teamID' => $input->teamID
));
}
在它看来,属性也传递到Post
选项卡中的POST响应,但在Response
和JSON
他们不露面只playerName
ATTR。
后
//source
{"playerName":"Joesph","teamID":"1"} // Good
但在
响应
//It gets an id for the table, a smart feature from backbone.js
{"playerName":"Joesph","id":23} // Missing "teamID":"1"?
只是额外的措施,这里是我的形式。
新玩家
<label>First & Last Name</label>
<input name="playerName" type="text" value="">
<hr />
// Here I tried dynamic like below and static, just passing in a number like "2"
<input type="hidden" name="teamID" value="<%= team.teamID %>" />
<button type="submit" class="btn">Create</button>
与上面的
playerName
我使用
teamID
试过的地方的形式,并在模型上其他属性
而且,没有什么工作,但attr属性?