weixin_33711641 2015-10-04 18:54 采纳率: 0%
浏览 40

ajax Post不起作用

I want to store an object in a restful server. I tried with an html form and everything works correctly but using javascript it doesn't work.This is the code

var app2={"user_id" : seleVal, "name":nome2, "img":img2, "type":tipo2, "supplier_id": distro}

            $.ajax({
                    type: "POST",
                    url: 'http://localhost:8000/products',
                    data: app2,
                    success: alert("success"),
                    error: alert("error"),
                    }); 

the code get into success and error at the same time so I tried to catch the error making functions inside them but when I do that success and error seem to don't responde.Thank you for the help

  • 写回答

3条回答 默认 最新

  • weixin_33721344 2015-10-04 18:57
    关注

    the code get into success and error at the same time

    These two lines are causing that:

    success: alert("success"),
    error: alert("error"),
    

    Note how you are actually calling the function alert() in each statement. Do this instead:

    ...
    success: function() { alert("success") },
    error: function() { alert("error") },
    ...
    
    评论

报告相同问题?