Juan Camilo Mejia
Juan Camilo Mejia

Reputation: 1012

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?

Upvotes: 1

Views: 52

Answers (1)

Max Koretskyi
Max Koretskyi

Reputation: 105507

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.

Upvotes: 4

Related Questions