jQuery.ajax returns 400 Bad Request

前端 未结 5 1818
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 10:06

This works fine:

jQuery(\'#my_get_related_keywords\').click(function() {
    if (jQuery(\'#my_keyword\').val() == \'\') return false;
        jQuery.getJSON(         


        
5条回答
  •  无人及你
    2020-12-09 10:19

    Late answer, but I figured it's worth keeping this updated. Expanding on Andrea Turri answer to reflect updated jQuery API and .success/.error deprecated methods.

    As of jQuery 1.8.* the preferred way of doing this is to use .done() and .fail(). Jquery Docs

    e.g.

    $('#my_get_related_keywords').click(function() {
    
        var ajaxRequest = $.ajax({
            type: "POST",
            url: "HERE PUT THE PATH OF YOUR SERVICE OR PAGE",
            data: '{"HERE YOU CAN PUT DATA TO PASS AT THE SERVICE"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json"});
    
        //When the request successfully finished, execute passed in function
        ajaxRequest.done(function(msg){
               //do something
        });
    
        //When the request failed, execute the passed in function
        ajaxRequest.fail(function(jqXHR, status){
            //do something else
        });
    });
    

提交回复
热议问题