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

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") },
...

Look this:

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

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: function(){
    alert("success");
  },
  error: function(){
    alert("error");
  }
});

</div>