0
我有一個非常簡單的方法在表中創建一個新記錄。但我只想要登錄用戶允許的這種操作(Laravel/Confide)。所有工作正常,無需檢查用戶是否已登錄。該腳本很好地將JSON字符串返回給我的jQuery Ajax調用。Laravel 4與Confide Auth :: check()混淆了JSON響應
但是,只要我添加'if(Auth :: check()'檢查,我得到一個巨大的額外字符串在結果中(完整的用戶類!?!?!與JSON編碼數組附加到它) ,搞亂了我的JSON編碼數組這是正常的行爲,還是我失去了一些東西
這是工作方法:?
public function create()
{
//if(Auth::check()) {
//$userId = Auth::getUser()->id; // Get logged in user id
$folder = new Folder();
$folder->parent_id = Input::get('fid');
$folder->title = 'Nieuwe map';
$folder->alias = 'nieuwe-map-'.time();
//$folder->created_by = $userId;
$folder->save();
return Response::json(array('success'));
//} else {
//return Response::json(array('error'));
//}
}
這是結果:
["success"]
當我添加認證檢查我得到這個結果:
<!--
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->remember_token;
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->remember_token = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return 'remember_token';
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
} -->["success"]
您介意發佈'json'視圖文件的內容嗎?如果你只是想發送一個json響應,你不能只是'返回Response :: json('json'=> ['success'])'? https://gist.github.com/anonymous/01b39d781945fff6bdba – thpl
<?php header('Content-type:application/json'); ?> {{json_encode($ json)}} –
您不應該在視圖中設置內容類型。我不知道laravel在內部對視圖做出反應時的全部過程,但是有些後臺處理以您的方式發生的可能性很高。你最好使用'Response :: json()'。看看我在第一條評論中提供的代碼是否適合你。 – thpl