weixin_33696822 2019-12-26 22:09 采纳率: 0%
浏览 362

Ajax请求的动态URL

I'm trying to create a search field on the top navbar with ajax. Since it's on the navbar, it has to be present on all pages, therefore the URL is constantly changing.

$.ajax({
            type: 'GET',
            url: CURRENT_URL,
            dataType: 'json',
            data: {
                search: userSearch
            },
            success: function (){...

I'm working with Laravel, so i tried this on the navbar page:

<script>
            var CURRENT_URL = "{{url()->current()}}"
</script>

The CURRENT_URL is displayed fine if i try to console log it, but ajax gives an error "Source map error: Error: request failed with status 404". How can i insert the current URL into the ajax request?

  • 写回答

2条回答 默认 最新

  • weixin_33735676 2019-12-27 02:23
    关注

    Hopefully, this can help. Here's my approach to make my url dynamically I get the URL in every forms var URL = $('#example_form').prop('action'); then the URL variable must append or set via Js/Jquery. check my example below.

    <script type="text/javascript">
    var URL = $('#example_form').prop('action');
    $.ajax({
          type:'GET',
          url: URL+'/clearContent',
          beforeSend: function (xhr) {
            var TOKEN = $('meta[name="csrf-token"]').attr('content');
            if (TOKEN) {
              return xhr.setRequestHeader('X-CSRF-TOKEN', TOKEN);
            }
          },
          data:{
            get_category_id : $('.parent-id').val(),
          },
          success:function(data){
            if (data.response.status == true) {
              // your codes here
            }
          }, 
          dataType: 'json',
          complete: function () {}
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!