私はLaravel 54を使用しています。私はajaxリクエストを使用してフォームを表示しようとしています。 jsonレスポンスを返しますが、java-scriptの受信データは2つのオブジェクトを含む文字列です!! Javaのスクリプトでajaxリクエストのビューを返す
自分の行動
public function edit($product)
{
$product = Product::where(['id'=>$product])->get();
$categories=Category::orderBy('name')->get();
var_dump($categories);
$view=view('product.manage',compact('categories','product'));
$html=$view->render();
return response()->json(['success'=>true,'html'=>$html]);
}
のjavaスクリプトコード
$(document).on('click','.edit-product',function(e){
e.preventDefault();
var url=$(this).attr('href');
$.ajax({
url:url,
type:'get',
success:function(response){
$('#data-holder').html(response.html);
},
error:function(e){
alert(e);
}
});
応答変数は次のとおりです。
"object(Illuminate\Database\Eloquent\Collection)#212 (1) {
["items":protected]=>
array(8) {
[0]=>
object(App\Category)#213 (24) {
["connection":protected]=>
string(5) "mysql"
["table":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["keyType":protected]=>
string(3) "int"
["incrementing"]=>
bool(true)
["with":protected]=>
array(0) {
}
["perPage":protected]=>
int(15)
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
["attributes":protected]=>
array(2) {
["id"]=>
int(5)
["name"]=>
string(15) "Beauty & Health"
//................................
}
}
}
}
{"success":true,"html":"<div class=\"container\">\n <div class=\"row\">\n //........... \n <\/div>\n<\/div>"}"
どのようにして返されたビューのHTMLにアクセスすることができますか?
'var_dump($ categories);'? :) – Peter