user1532944
user1532944

Reputation: 133

AJAX get then redirect after success

I am trying to use ajax to GET form data to send to api url then redirect the user who filled out form to a thankyou page.

<form id="valform">
<!-- form elements -->
</form>
<div id="status_message"></div>

Simple form code is above and the ajax code is under

$(document).ready(function(){
var API_URL = 'http://www.some-api-provider.com/api.php';    
$('#valform').on('submit', function (e){
e.preventDefault();

$.ajax({
  type: 'GET',
  url: API_URL,
  data: $('#valform').serialize(),
  success: function () {
    window.location.href = "thankyou.html";

  },
  error: function () {
    alert('There was a problem!'); // do something better than this!
  }
});
return false;
});

Is there problems with this code? I keep getting the alert message in the GET section so i'm assuming something is wrong in there.

Upvotes: 0

Views: 2421

Answers (1)

Polyov
Polyov

Reputation: 2311

Unless your file is hosted on the same domain as the API_URL, AJAX will stop your request, That is why you are seeing the error message. This is to stop XSS (Cross Site Scripting), a hacking technique. This is called the same-domain policy.

Upvotes: 1

Related Questions