2017-04-18 65 views
1

我正在使用Laravel 54。我尝试使用ajax请求显示表单。 我返回一个json响应,但在java-script中接收到的数据是一个包含两个对象的字符串!在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?

+0

'var_dump($ categories);'? :) – Peter

回答

0

删除var_dump($categories);部分,因为该部分打印对象的内容。

0

laravel默认渲染的部分。您正在调用View::make()并将呈现的视图返回到$html