ajax中的服务器响应是错误功能而不是成功

时间:2016-10-05 09:06:48

标签: javascript jquery ajax

我已经看到了上述问题的许多解决方案,但没有一个属于我的类别。我的服务器正在响应正确的json。无法确定导致问题的原因。我将状态显示为0,状态文本显示为错误。那真令人恶心 。问题在于deleteimage()函数。 mi8在处理事件的方式上有问题。如果有人能弄清楚它会很好。在此之后差不多有一天。感谢帮助。

 $(document).ready(function() {
    //grab all form data  
        var form = $('#get_design_form');

        form.submit(function(event) {                   
            event.preventDefault();         
            var x = true;


            if(category_id.value == '0'){
                alert("Please select category.");
                category_id.focus();
                x=false;
                return x;
            }

            if(design_no.value == ''){
                alert("Please enter design no.");
                design_no.focus();
                x=false;
                return x;
            }



            if (x) {
                $('#send').attr("disabled",true);
                $('#lm').show();
                $.ajax({
                    type: form.attr('method'),
                    url: form.attr('action'),
                    data: form.serialize(),
                    success : function(data) {
                        var result = data;


                        if(result.response_code=='101'){
                            $('#failure').show();
                            $('#success').hide();
                            $('#failure').text("Error Occured.Try again");
                        }else if(result.response_code=='102'){
                            $('#failure').show();
                            $('#success').hide();
                            $('#failure').text("Design no. does not exist.");
                        }else{
                            $('#con2').show();
                            $('#con1').hide();

                            $('#category_name').val(result.design_details.category_id);                         
                            $('#des_no').val(result.design_details.design_no);

                            //$('#image').attr('src','downloadImage?image_name='+result.design_img);

                            for(i=0;i<result.design_details.list_design_img.length;i++){

                                var html='<img id="'+result.design_details.list_design_img[i].design_img_path.split(".")[0]+'" src=downloadImage?image_name='+result.design_details.list_design_img[i].design_img_path+' style="vertical-align:middle;" width="100" height="100">';

                                var html1='<span style="cursor:pointer;" onclick="javascript:deleteimage(\'' + result.design_details.list_design_img[i].design_img_path + '\',\'' + result.design_details.category_id + '\',\'' + result.design_details.design_no + '\')">X</span>';


                                $('#img_div').append(html);
                                $('#img_div').append(html1);

                            }


                        }               
                        $('#lm').hide();
                        $('#send').attr("disabled",false);
                    }
                });
            }
        });
                        //return false;
        $('#send').attr("disabled", false);
        $('#lm').hide();
    });


<script type="text/javascript">
function deleteimage(image_name,category_id,design_no)
{
    alert("hii"+image_name+","+category_id+","+design_no);
    var answer = confirm ("Are you sure you want to delete?");


    if (answer)
    {   
            $.ajax({                    
                    url: "http://192.168.0.2:8080/JaydeepSarees/deleteOthersDesignImgPath",
                    data: jQuery.param({image_name: image_name,category_id: category_id,design_no: design_no}) ,
                    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                    type: "post",                   
                    success: function (data) {
                        alert("hii");
                        var result=data;
                        alert(result.response_code);
                        if (result.response_code == 100) {
                            alert(result.response_code);
                            $("#"+image_name).remove();
                        }                     
                    },error: function (jqXHR,exception) {

                        console.log(jqXHR);                     
                        var msg = '';
                        if (jqXHR.status === 0) {
                            msg = 'Not connect.\n Verify Network.'+jqXHR.statusText;
                        } else if (jqXHR.status == 404) {
                            msg = 'Requested page not found. [404]';
                        } else if (jqXHR.status == 500) {
                            msg = 'Internal Server Error [500].';
                        } else if (exception === 'parsererror') {
                            msg = 'Requested JSON parse failed.';
                        } else if (exception === 'timeout') {
                            msg = 'Time out error.';
                        } else if (exception === 'abort') {
                            msg = 'Ajax request aborted.';
                        } else {
                            msg = 'Uncaught Error.\n' + jqXHR.responseText;
                        }

                        alert(msg);
                    }
                });
      }
    return false;
}
</script>

1 个答案:

答案 0 :(得分:0)

我尝试向服务器发送请求时需要使用相对路径,其中i使用绝对路径,这是我获得状态为0的原因。

url: "deleteOthersDesignImgPath"

而不是

url: "http://192.168.0.2:8080/JaydeepSarees/deleteOthersDesignImgPath",