2014-02-14 58 views
0

我完成這篇Laravel 4 REST類型的教程在這裏 - http://code.tutsplus.com/tutorials/laravel-4-a-start-at-a-restful-api-updated--net-29785調用Laravel在瀏覽器網址4寧靜的捲曲

我想要做的就是測試瀏覽器的URL鏈接寧靜:

curl命令:

$ curl -i --user firstuser:first_password -d 'url=hxxp://google.com&description=A Search Engine' www.lrvlapp.com/api/v1/url 

這是運行正常,並返回預期的JSON:

{"error":false,"message":"URL created"} 

瀏覽器網址:我試試這個:

www.lrvlapp.com/api/v1/url?url=hxxp://google.com&description=A Search Engine 

沒有錯誤或任何東西,並給出任何URL插入到數據庫中。

這是UrlController

類UrlController擴展\ {BaseController

/** 
* Display a listing of the resource. 
* 
* @return Response 
*/ 
public function index() 
{ 
    return 'Hello, API'; 
} 

/** 
* Show the form for creating a new resource. 
* 
* @return Response 
*/ 
public function create() 
{ 
    // 
} 

/** 
* Store a newly created resource in storage. 
* 
* @return Response 
*/ 
public function store() 
{ 
    $url = new Url; 
    $url->url = Request::get('url'); 
    $url->description = Request::get('description'); 
    $url->user_id = Auth::user()->id; 

    // Validation and Filtering is sorely needed!! 
    // Seriously, I'm a bad person for leaving that out. 

    $url->save(); 

    return Response::json(array(
     'error' => false, 
     'message' => 'URL created') 
    ); 
} 

/** 
* Display the specified resource. 
* 
* @param int $id 
* @return Response 
*/ 
public function show($id) 
{ 
    // 
} 

/** 
* Show the form for editing the specified resource. 
* 
* @param int $id 
* @return Response 
*/ 
public function edit($id) 
{ 
    // 
} 

/** 
* Update the specified resource in storage. 
* 
* @param int $id 
* @return Response 
*/ 
public function update($id) 
{ 
    // 
} 

/** 
* Remove the specified resource from storage. 
* 
* @param int $id 
* @return Response 
*/ 
public function destroy($id) 
{ 
    // 
} 

}

www.lrvlapp.com實際上是通過主機我的虛擬主機我的設置文件和Apache虛擬主機

實際位置是:c:\ xampp \ htdocs \ lrvlapp \ public

謝謝你的回答

+0

你只是得到一個空白的屏幕? – Mediabeastnz

+0

是的,拿到了空白屏幕 –

+0

你是什麼把一個回聲'你好';死();在函數store()中。這會得到迴應嗎? – Mediabeastnz

回答

1

當你通過瀏覽器請求時,你發出一個GET請求,然後調用索引方法。 您需要提交POST請求以保存網址。然後它將調用商店方法。這是節約資源的標準方式。

你可以在doc更澄清:http://laravel.com/docs/controllers#resource-controllers

編輯: 如果你想在這種情況下使用jQuery的AJAX調用(POST),你可以調用象下面這樣:

$.ajax({ 
      type: 'POST', 
      url: 'www.lrvlapp.com/api/v1/url', 
      data: { url: "http://google.com", description: "A Search Engine" }, 
      dataType: 'json', 
      success: function(data) { 
       <do your actions, for example show some message>   
       $('#div-id').html(data); 
      }, 
      error: function(jqXHR, textStatus, errorThrown) { 
       $('#div-id').html('Error: , ' + textStatus + ', ' + errorThrown); 

      } 
     }); 
+0

我想通過AJAX爲SPA調用Restfull方法。任何有關要調用的URL的建議?這就是我想要的答案 –

+0

Laravel網站上的文檔對於像我這樣的人來說太簡約了。我通過示例和教程學習。 –

+0

你好,我可以通過ajax調用restful方法嗎? – Shafiul