【问题标题】:$.get how to add headers using laravel$.get 如何使用 laravel 添加标题
【发布时间】:2021-12-15 01:57:17
【问题描述】:

我想添加这个标题

headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
},

到我的$.get jquery 请求

$('.wilayas').on('change', function(x) {
        /*             
                    $('.dayras').find('option')
                        .remove()
                        .end() */
        alert(this.value)

        $.get("{{ route('customer.auth.dayras') }}", {
                data: this.value
            },
            function(data, status) {
                alert(data);
                $('.dayras').append(`
             <option value="-">-</option>
        `);
                data.map(dd => {
                    $('.dayras').append(`
             <option value="${dd.id}">${dd.dayra}</option>
        `);
                })
            });
    });

我得到的是 html 响应而不是 json

【问题讨论】:

  • 你不能使用fetch api吗?
  • 试试$.get( "test.php", your_data, your_fn, "json")

标签: javascript ajax laravel


【解决方案1】:

jQuery ajax headers 选项的语法:

$.ajax({ 
    headers: {          
        "Accept": "text/json"
        "Content-Type": "application/json"
    }    
    ...
    ...
});

jQuery ajax headers 选项的语法设置或覆盖beforeSend 回调函数:

$.ajax({
    url: "{{ route('customer.auth.dayras') }}",
    data: { foo: bar },
    type: "GET",
    beforeSend: function (jqXHR, settings) { 
        jqXHR.setRequestHeader("Accept", "text/json");
        jqXHR.setRequestHeader("Content-Type", "application/json");
    }
    ...
    ...
});

【讨论】:

  • 我想使用 $.get 而不是 $.ajax
  • @ismailtaibi 我想你需要了解更多关于 jquery 的知识。
猜你喜欢
  • 1970-01-01
  • 2020-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 1970-01-01
  • 2017-10-13
  • 2019-05-09
  • 1970-01-01
相关资源
最近更新 更多