【问题标题】:Invoking Laravel 4 Restful curl in browser URL在浏览器 URL 中调用 Laravel 4 Restful curl
【发布时间】:2014-02-14 01:18:45
【问题描述】:

我在这里完成了 Laravel 4 Restfull 教程 - http://code.tutsplus.com/tutorials/laravel-4-a-start-at-a-restful-api-updated--net-29785

我要做的是测试浏览器URL中的Restful链接:

卷曲命令:

$ 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 实际上是我通过 hosts 文件和 apache 虚拟主机设置的虚拟主机

实际位置在:c:\xampp\htdocs\lrvlapp\public

谢谢你的回答

【问题讨论】:

  • 你只是得到一个空白屏幕吗?
  • 是的,得到了​​空白屏幕
  • 你放什么 echo 'hello';死();在函数 - store() 中。这会得到回应吗?
  • 我这样做了,但现在变得更奇怪了,我得到的是“你好,API”而不是空白屏幕 - 似乎它调用了 index() 函数
  • 如果你试试 www.lrvlapp.com/api/v1/url/store?url=hxxp://google.com&description=A 搜索引擎

标签: laravel-4 restful-url


【解决方案1】:

当您通过浏览器请求时,您发出的是 GET 请求,该请求又调用 index 方法。 您需要提交 POST 请求以保存 url。然后它将调用 store 方法。这是节省资源的标准方式。

您可以在文档中获得更多说明: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);

            }
        });

【讨论】:

  • 我想要的是通过 AJAX 为 SPA 调用 Restfull 方法。对要调用的 URL 有什么建议吗?这就是我想要的答案
  • Laravel 网站上的文档对于像我这样的人来说太简约了。我通过示例和教程学习。
  • 嗨,你能像我提到的那样通过ajax调用restful方法吗?
  • 不,我想我知道问题出在哪里。因为调用 URL 时需要进行身份验证。直接输入网址,会跳过认证。本教程适用于服务器端通过身份验证访问 REST 服务的不同应用程序
  • 对于url,是客户端通过AJAX调用服务器,不需要认证,可以通过web登录和session来完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 2019-02-25
  • 1970-01-01
  • 2019-10-29
  • 2020-08-13
  • 2014-03-14
相关资源
最近更新 更多