Jump to content

[SOLVED] Ajax/urlsegment working on localhost but not on server


louisstephens
 Share

Recommended Posts

So I have a template called "development" where I am testing out a few ideas etc set up on a local mamp server. I also have a page called "ajax" using a template called ajax. From my development template, I am posting a form using ajax and all is working quite well:

$('.test').click(function(event) {
event.preventDefault();
redirectUrl = $(this).data('redirect');

var data = {
	firstName: $("#firstname").val(),
    lastName: $("#lastname").val(),
    email: $("#email").val(),
    phone: $("#phone").val(),
    redirectUrl: redirectUrl
};



$.ajax({
	type: "POST",
    url: "localhost:8888/sandbox/ajax/form/",
    data: data,
    success: function() {
	    console.log(this.data);
        top.window.location = redirectUrl;
    },
    failure: function() {
	    console.log(this.data);
    }
});

And in my ajax template:

if($input->urlSegment == 'form'){

	if ($_POST) {
		//handle the post
	}
}

The redirectUrl is a data attribute I added in to the button (that launches the form in a modal) that pulls in from a field in processwire. Everything works just fine locally, but when I export everything to a live site it fails. It will work 1 time and redirect, but if you go back to resubmit, it submits the data but won't redirect. Can anyone see any glaring issue here, or have a better way around this?

------------------------------

So, it turns out the request to /ajax/form/ was being canceled. It still handled the POST request, but never sent back a 200 to show "success". Adding in 

return false;

at the end of my .onclick solved the issue. It is strange though as I have never had this issue before.

Edited by louisstephens
figured out a solution
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...