2014-02-14 56 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