简体   繁体   English

如何在Ajax中获得错误响应?

[英]How to get error response in Ajax?

I am trying to add data to a cart(using) Ajax jQuery, now I have added a primary key constraint and I want when the constraint is violated in get the error message in jQuery: 我正在尝试将数据添加到使用Ajax jQuery的购物车中,现在我已经添加了主键约束,并且想要在jQuery中获取错误消息时违反约束:

 function add()
       {
        $(document).ready(function() 
        {
        $('#addtocart').submit(function() {
           //$('#add-button').prop('disabled',true);
    var user = $('#user').val();
           var pid = $('#pid').val();
        $.ajax({
        type:       "post",
        url:        "/devilmaycry/register?action=addtocart",
        data:       {pid:pid ,user:user},
        success:    
                   function() 
                    {
                          alert("Item has been added to cart");
                },
                  error:
                     function() 
                    {
                          alert("Item already present in the cart");
                } 
              });
     return false;
   });

        });
       }   

The error function never runs, the functionality in the DB is running fine but I don't see the error message in Ajax. 错误功能永远不会运行,数据库中的功能运行良好,但是我在Ajax中看不到错误消息。

Here is Java code: 这是Java代码:

public int addintocart(String user,int pid)
{
 try
  {
   conn = obj.connect();
   String sql="insert into cart(userid,product_id,quantity)  values(?,?,?)"; 
   ps1 = conn.prepareStatement(sql);
        ps1.setString(1,user);
        ps1.setInt(2,pid);
        ps1.setInt(3,1);

         ps1.executeUpdate();

  }
    catch(SQLException e)
    {
        e.printStackTrace();
    }
    catch(Exception k)
    {
        k.printStackTrace();
    }

   return x;
 }

What is going wrong? 怎么了?

You have to send the error back as a servlet response from Java. 您必须将错误作为Java的Servlet响应发送回去。 Here is an example: How to return Java Exception info to jQuery.ajax REST call? 这是一个示例: 如何将Java异常信息返回给jQuery.ajax REST调用?

Also, you are missing some parameters in the ajax error function. 另外,您在ajax错误函数中缺少一些参数。 It might be another problem. 这可能是另一个问题。 Check out http://api.jquery.com/jquery.ajax/ You should evaluate the error and present the appropriate message. 查看http://api.jquery.com/jquery.ajax/您应该评估该错误并显示相应的消息。

That error code wwill never work. 该错误代码将永远无法工作。 the onerror event of AJAX only runs when connection cannot be established or URL does not exist. 仅当无法建立连接或URL不存在时,AJAX的onerror事件才会运行。 You have to return something from your the server indicating success or faliure. 您必须从服务器返回一些指示成功或失败的信息。

I would do it this way. 我会这样做。

PHP(convert this code to JAVA): PHP(将此代码转换为JAVA):

if(success){
    die(1);
} else {
    if(notyetincart){
       die(0);
    } else {
        die(2);
    }
}

Then Jquery coded: 然后,jQuery编码为:

function add()
       {
        $(document).ready(function() 
        {
        $('#addtocart').submit(function() {
           //$('#add-button').prop('disabled',true);
    var user = $('#user').val();
           var pid = $('#pid').val();
        $.ajax({
        type:       "post",
        url:        "/devilmaycry/register?action=addtocart",
        data:       {pid:pid ,user:user},
        success:    
                   function(response) 
                    {
                          if(reponse==="1")
                          alert("Item has been added to cart");
                          else if(reponse==="0")
                          alert("Item could not be added to cart");
                          else alert("Item already present in the cart");

                },
                  error:
                     function() 
                    {
                          alert("Item could not be added to cart due to poor network connection");
                } 
              });
     return false;
   });

        });
       }

Hope this helps 希望这可以帮助

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

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