简体   繁体   English

Ajax调用总是以错误处理程序结尾

[英]Ajax call always end up in error handler

I am trying to get a text value('selected crate') from the server using an ajax call. 我正在尝试使用ajax调用从服务器获取文本值(“选定的板条箱”)。 Ajax call is: Ajax的调用是:

var selected_crate ='';

$.ajax({
    url: OC.linkTo('crate_it', 'ajax/bagit_handler.php')+'?action=get_crate',
    type: 'get',
    dataType: 'text/html',
    success: function(data){
        selected_crate = data.responseText;
        $('#crates option').filter(function(){
            return $(this).attr("id") == selected_crate;
        }).prop('selected', true);
    },
    error: function(data){
        var e = data.responseText;
        alert(e);
    }
});

And the server side code snippet is: 服务器端代码段为:

case 'get_crate':
    $msg = $bagit_manager->getSelectedCrate();
    print $msg;
    break;

I want to do something upon success but this call always end up in error handler. 我想在成功时做点什么,但是此调用总是以错误处理程序结束。 If there were complete handler, it would go in that handler. 如果有完整的处理程序,它将进入该处理程序。 But I want to use both success and error handlers because I want to 但是我想同时使用成功和错误处理程序,因为我想

  1. Send error response if something is wrong from the server side 如果服务器端出现问题,则发送错误响应
  2. Do something on success in the client side 在客户端取得成功

I am struggling to achieve this. 我正在努力实现这一目标。 Why this call always end up in error handler and how can I actually send an error response with regard to this call that would end up in error handler if any error occurs otherwise success response? 为什么此调用总是以错误处理程序结尾,并且我如何才能针对该调用发送错误响应,如果发生任何错误,该调用将以错误处理程序结尾,否则会成功响应?

查看是否在错误处理程序中检索到数据,或者是否正确检索到数据,这意味着您的数据类型与ajax调用中的响应不匹配[请参阅服务器代码,在这种情况下,它必须返回一些额外的值]

url: OC.linkTo('crate_it', 'ajax/bagit_handler.php')+'?action=get_crate', 网址:OC.linkTo('crate_it','ajax / bagit_handler.php')+'?action = get_crate',

in place of this try directly url like 代替这个尝试直接URL像

url: www.yoursite.com/ajax/bagit_handler.php?action=get_crate 网址:www.yoursite.com/ajax/bagit_handler.php?action = get_crate

i think it will help you to get Sucess. 我认为这将帮助您获得成功。

If the URL is right then try this: 如果网址正确,请尝试以下操作:

dataType: "html"

See: http://api.jquery.com/jQuery.ajax/ 参见: http : //api.jquery.com/jQuery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM