【问题标题】:Laravel Jquery Ajax 404 error and migration URL IssueLaravel Jquery Ajax 404 错误和迁移 URL 问题
【发布时间】:2023-12-20 17:52:01
【问题描述】:

我在使用 laravel 和 Ajax 时遇到错误。

这是我的代码。

 <button id="openBtn" class="btn blue pull-right"><i class="glyphicon glyphicon-plus"></i> Add Certification</button>

$(".editcertifictns #openBtn").click(function(e){
e.preventDefault();
$.ajax({
  type:'get',
  dataType:'html',
  async:false,
  url:'getcertifications',
  success:function(response){
    $("select[name=certictnsoptn]").html(response);
    $(".modelPophldr,.modelPop").show();
    $("#catagry_name").val("").focus();
    $(".formitmpic").hide();
    $(".modelPop").animate({'margin-top':'25px'});
  },
});

路线。

 Route::get('getcertifications',['uses'=>'GblgetapiController@getcertifications']);

控制器

public function getcertifications(){
    $arr = DB::table('master_cirtfctnassc')->get();
    $string ="";
    foreach ($arr as $arr) {
        $string .= "<option value='.$arr->cirtfctnassc_name.'>".$arr->cirtfctnassc_name."</option>"
    }
    return $string;
}

装订区..

  <div class="modelBody">
      <div class="form-group">
        <select class="form-control" name="certictnsoptn">
          <option></option>
        </select>
      </div> 
    </div>

当点击按钮时,应用会发送一个ajax..

我附上ajax执行时firebug的截图...

查看悬停的 URL...:(((((

如何在 js 文件中使用完整的 url 而不会在迁移阶段产生问题.... 任何人请帮我解决这个问题???? :(:(:(:(:(:(:(:(

【问题讨论】:

  • 你有 jQuery 代码的&lt;script&gt; 标签吗?
  • 它包含一个 jquery 文件代码老兄......
  • 那么你在上面展示它的方式然后你的js中有HTML,老兄!但是考虑到这个问题,为什么不使用相对 URL?或者,使用基本 URL *.com/questions/23059918/laravel-get-base-url
  • 我想要 js 文件中的完整 url...而不是在 laravel 控制器或布局中...当我使用 url() 方法时,它会出现错误:((((
  • 在控制器 getcertifications() 中生成 HTML 代码并不酷。

标签: php jquery ajax laravel-5 response


【解决方案1】:

要使完整 URL 在您的所有 Javascript 中可用,您可以在页面中的某处设置一个全局变量,如下所示(在刀片中):

<script type="text/javascript">
   var BASEURL = "{!! url('/') !!};";
</script>

所以以后你可以这样做:

$.ajax({
  type:'get',
  dataType:'html',
  async:false,
  url: BASEURL + 'getcertifications'
});

【讨论】: