dongpa5277 2014-03-23 22:33
浏览 65
已采纳

缩短url函数,需要两个ajax请求才能返回缩短的url字符串

Using the Laravel framework, I made a few functions to generate shortened urls, the function works (almost), I have to do the ajax call twice to return the shortened url.

First ajax call receives a 500 error.

enter image description here

And here's the successful call the second time:

enter image description here

I'm confused how a function can work (but not really) at the same time.

EDIT (additional info):

/shorten is used for routing:

Route::post('/shorten', array('uses' => 'UrlController@shorten'));

functions:

Which further calls the shorten function in the UrlController:

public function shorten(){

    header('Content-type: text/plain');

    $long_url = $_POST['url'];
    $link = Link::checkOrShorten($long_url);

    return $link->short_url;
}

Ajax:

$(function() {
    $('#litlingit').click(function(){
      $.ajax({
        type: "POST",
        data: { url: $('input[name="long_url"]').val() },
        dataType: "text",
        url: "/shorten"
      }).done(function( data ){
        $('input[name="long_url"]').val('http://litl.it/'+data);
      });
  });
});

Module:

public static function checkOrShorten($long_url)
{

    $link = new Link;
    $link->users_id = NULL;

    if (Auth::check())
    {
        $link->users_id = Auth::user()->id;
    }

    $link->long_url = $long_url;
    $query = DB::select("select short_url from links where long_url = '$long_url'");
    if ($query) 
    {
        foreach ($query as $links)
        {
            $link->short_url = $links->short_url;
        }   
        $url_id = DB::select("select id from links where long_url = '$long_url'");
        DB::table('users_links')->insert(array('link_id' => $url_id[0]->id, 'user_id' => $link->users_id, 'privacy' => 0));
    }

    while(is_null($link->short_url)) 
    {
        $link->short_url = str_random(4);
        $query = DB::select("select short_url from links where short_url = '". $link->short_url ."'"); 

        if(!$query)
        {
            $link->save();
            $url_id = DB::select("select id from links where short_url = '$link->short_url'");
            DB::table('users_links')->insert(array('link_id' => $url_id, 'user_id' => $link->users_id, 'privacy' => 0));
        }
    }
    return $link;
}
  • 写回答

2条回答 默认 最新

  • dptj13337 2014-03-24 23:32
    关注

    Your shortener is working well, but in your first request an exception is being raised in Laravel:

    {"error":{"type":"ErrorException","message":"ksort() expects parameter 1 to be array, null given","file":"\/var\/www\/litl.it\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Query\/Builder.php","line":1407}}
    

    This error returns as a 500 error to javascript, that's normal.

    But it creates the shortened url anyway, so in the second request it just returns the value you already have in database.

    You should have information (call stack) about this in your Laravel log, run

    php artisan tail
    

    And try to shorten another one. As your site is live, I was able to do some tests online using Postman Chrome Extension: http://puu.sh/7IfXJ.png.

    You might have a problem with this part of your code:

        if(!$query)
        {
            $link->save();
            $url_id = DB::select("select id from links where short_url = '$link->short_url'");
            DB::table('users_links')->insert(array('link_id' => $url_id, 'user_id' => $link->users_id, 'privacy' => 0));
        }
    

    You are looking for a link as soon as you saved it in database and sometimes you might get a null, but, since you are saving it and Laravel tries to get the id back into your model, I think you can change it to:

        if(!$query)
        {
            $link->save();
    
            DB::table('users_links')->insert(array('link_id' => $link->id, 'user_id' => $link->users_id, 'privacy' => 0));
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?