Juan Camilo Mejia Juan Camilo Mejia - 1 month ago 9
AngularJS Question

Difference betwen html post and js http.post

I have a html form like this:

<form method="post" action="https://serviceURL/">
<input name="account" ng-value="form.account" type="hidden">
<input name="name" ng-value="form.name" type="text">
<button type="submit">Continue</button>
</form>


When I submit the form all goes perfect. But when I try to do the same post request inside an angular service I'm getting


cannot load https://serviceURL/ Response for preflight has invalid HTTP status code 504


my service function looks like this

service.callServer =function(){
var query = "https://serviceURL/";
$http.post(query,{account:123,name:'jhon'});

}


My question is: What is the difference betwen those types of request? does the html action post add some headers that the http.post doesn't?

Answer

It's most likely because of CORS. Ajax requests cannot be submitted to domains other than the one you're on, unless the server sets Access-Control-Allow-Origin header specifying your domain. To validate that, a browser first sends preflight OPTIONS request, and it seems that your server responds with an error to that request. This doesn't apply to submitting html forms, since it's not AJAX request.