0

I have a form that when submitted, goes to blah.php. The problem is, blah.php goes off site to another domain. JQuery sees that and gives a 302 Object Moved error. So I had to use JSON and AJAX to send the form. Comment details are within the Jquery code below.

The flow should be click button, check server side, if not 'ok' response, output response in status div and stop everything server side on that page. If 'ok' was the response let form continue on its way

Quick mock up of my code

<form id="ppform" method="post" action"blah.php">
<input id="someid" type="text" />
<button id="sendbutton">Send</button>
<div id="status"></div>
</form>

$(document).ready(function(){
    $(document).on('click', '#sendbutton', function(){
         $('#status').empty();
         $.ajax({
             type: "POST",
             url: "blah.php",
             data: reqBody,
             dataType: "json",
             success:function(data,textStatus){ 
                  // here I want the div to return data if the response isn't 'ok'
                  if(data!='ok'){
                      $('#status').append(data);
                  }else{
                      // response was 'ok' so empty div, 
                      // show loading gif and submit the form
                      $('#status').empty().html('<div id="proc">Processing</div><img src="loading.gif" />');
                      if (data.redirect){
                         window.location.href = data.redirect;
                      } else {
                         $("#ppform").replaceWith(data.form);
                      }
                  }
               }
         });
    }); 
});
Prasenjit Kumar Nag
  • 13,391
  • 3
  • 45
  • 57
Patriotec
  • 1,104
  • 4
  • 22
  • 43
  • 1
    if blah.php is sending back a 302 status it means its probably using something like header('Location: someoOtherUrl'); Which isn't useful as an ajax server side script. Especially if its going to another server trying to call that redirect location is going to cause COR (cross origin request) problems. I would suggest rewriting your blah.php to not redirect, but instead passthru the content of the external page. – ehudokai May 24 '12 at 16:18
  • the page that its going to (blah.php), is doing the server side validation, then if validation passes, the page goes off to another site. The 302 object moved doesnt come till AFTER validation has passed. Right now, the form works except i cant output the data if validation stops everything. Instead of the div the validation error shows up on blah.php – Patriotec May 24 '12 at 16:40
  • what do you mean by " the page goes off to another site"? Is this being done using the header(...) call? Or is it being done on the javascript side where you are doing the window.location.href = data.redirect; . A 302 status is the result of a redirect on your php page. So without seeing your php page its hard to help more. – ehudokai May 24 '12 at 16:58
  • the code is for paypal, the page is redirected offsite to https:// so it creates an object moved error if i use jquery.form.js without the ajax. – Patriotec May 24 '12 at 17:02
  • So you don't control blah.php? – ehudokai May 24 '12 at 17:07
  • i control blah.asp, i dont control what happens after validation occurs, papypal takes over. I'm trying to output the validation errors before paypal takes over. Actually any validation problems, blah.php stops anything else from happening, paypal never comes into the mix if validation fails. – Patriotec May 24 '12 at 17:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11705/discussion-between-patriotec-and-ehudokai) – Patriotec May 24 '12 at 17:33

2 Answers2

0

I think, it is a browser security issue, that you can't send ajax request on different domain.

  • But if it's the server that does the cross domain request? – SuperSkunk May 24 '12 at 16:16
  • @ Rajeev: Not true, ajax is used for cross domain connections: http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/ – SteveLacy May 24 '12 at 16:16
0

If you're using jQuery to do the ajax requests you could change the datatype to JSONP to do cross domain json calls.

more information here:

jQuery AJAX cross domain

Community
  • 1
  • 1
Vince V.
  • 3,115
  • 3
  • 30
  • 45