ajax Post doesn't work

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

Answers


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");
  }
});

Need Your Help

Clean JSON from ServiceStack Service

c# json servicestack

I am evaluating ServiceStack and I have followed some examples. However, the JSON that is returned looks like { key: arrayofobjects } instead of just { arrayofobjects }.

varied htaccess mod_rewrite

php .htaccess mod-rewrite seo

I'm trying to get SEO sorted for my website but I can't seem to figure out this scenario.