weixin_33743880 2018-03-21 09:12 采纳率: 0%
浏览 70

jQuery Ajax无法捕获404 [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/1637019/how-to-get-the-jquery-ajax-error-response-text" dir="ltr">How to get the jQuery $.ajax error response text?</a>
                            <span class="question-originals-answer-count">
                                (11 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2018-03-21 09:43:44Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

I am trying to use AJAX to populate my dropdown list and I am returning a 404 with error message from my controller, and Ajax is not catching it...

My return at the controller is

return Response()->json(array('error' => '404 car type not found'), 404);

And here is my JS

$('document').ready(function () {
$('#car_type').bind('changed.bs.select', function () {
    $.ajax({
        type: 'POST',
        url:'carclass/'+$('#car_type').val(),
        dataType: 'json',
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert('status:' + XMLHttpRequest.status + ', status text: ' + XMLHttpRequest.statusText);
        },
        success: function( json ) {
           $.each(json, function(i, obj){
                $('#car_class').append($('<option>').text(obj.name).attr('value', obj.id));
           });
           $('#car_class').selectpicker('refresh');
        }
});
 });
 });

It is returning

GET http://localhost:8000/ads/cartype/2 404 (Not Found)
</div>
  • 写回答

2条回答 默认 最新

  • 关注

    Replace your error block with something like this where xhr.status will give you the status code of the response.

    error:function (xhr, ajaxOptions, thrownError){
        if(xhr.status==404) {
            alert('status:' + xhr.status + ', status text: ' + xhr.statusText);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥15 计算决策面并仿真附上结果
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么