相关疑难解决方法(0)

jQuery Ajax错误处理,显示自定义异常消息

有没有什么方法可以在我的jQuery AJAX错误消息中显示自定义异常消息作为警报?

例如,如果我想通过Struts by 在服务器端抛出异常throw new ApplicationException("User name already exists");,我想在jQuery AJAX错误消息中捕获此消息('用户名已存在').

jQuery("#save").click(function () {
  if (jQuery('#form').jVal()) {
    jQuery.ajax({
      type: "POST",
      url: "saveuser.do",
      dataType: "html",
      data: "userId=" + encodeURIComponent(trim(document.forms[0].userId.value)),
      success: function (response) {
        jQuery("#usergrid").trigger("reloadGrid");
        clear();
        alert("Details saved successfully!!!");
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

在第二个警报,我警告抛出的错误,我得到undefined,状态代码是500.

我不确定我哪里出错了.我该怎么做才能解决这个问题?

ajax jquery custom-exceptions

708
推荐指数
15
解决办法
140万
查看次数

jQuery ajax错误函数

我有一个ajax调用将数据传递给页面,然后返回一个值.

我已经从页面检索了成功的调用,但我编写了它,以便在asp中引发错误.我如何从jquery中检索该错误?

例如:

cache: false,
url: "addInterview_Code.asp",
type: "POST",
datatype: "text",
data: strData,
success: function (html) {
    alert('successful : ' + html);
    $("#result").html("Successful");
},
error: function (error) {
    **alert('error; ' + eval(error));**
}
Run Code Online (Sandbox Code Playgroud)

这是我不理解的错误位.在函数中我需要放置什么参数,以便我可以使用我在服务器中引发的错误消息.

ajax jquery

117
推荐指数
4
解决办法
43万
查看次数

Ajax成功和错误功能失败

我无法让我的jQuery ajax正常工作.它指向PHP页面以更新数据库,但永远不会返回脚本以获取成功或错误选项.

我的代码如下:

$(document).ready(function(){  
        $("form#updatejob").submit(function() {  
            function textreplace(x) {return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");}
            // we want to store the values from the form input box, then send via ajax below
            var job     = $("#job").attr("value");
            var description     = $("#description").val();
            description.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
            var startDate   = $("#startDate").attr("value");
            var releaseDate = $("#releaseDate").attr("value");  
            var status  = $("#status").attr("value"); 
            $.ajax({
                beforeSend:textreplace(description),
                type: "POST",  
                url: "updatedjob.php",
                data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, 
                success: function(){  
                    $("form#updatejob").hide(function(){$("div.success").fadeIn();});  
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) { …
Run Code Online (Sandbox Code Playgroud)

ajax jquery

41
推荐指数
3
解决办法
30万
查看次数

Jquery ajax错误回调

我在这里需要一些建议或者一些解释.我有一个jquery ajax调用,

$.ajax({
 type: "GET",
 url: base_url+'/ajax/fetch/counts/',
 dataType: 'json',
 data: {},
 error: function(xhr, error){
        console.debug(xhr); console.debug(error);
 },
 success: display_counts
});
Run Code Online (Sandbox Code Playgroud)

它工作正常.我的success回调在响应时正确触发.但是,我注意到的是error每次都会触发我的回调,即使我的调用返回成功状态200.在上面的error回调中,我看到该对象xhr.status是200.

任何人都可以解释什么是错的,或者这里发生了什么?error只有当我有404或非200响应时才会触发回调.我的假设是否正确?

谢谢.

ajax jquery

34
推荐指数
2
解决办法
9万
查看次数

标签 统计

ajax ×4

jquery ×4

custom-exceptions ×1