Welcome Guest, Not a member yet? Register   Sign In
Ajax issues with posting form
#1

[eluser]gregormck[/eluser]
I'm using Ajax to post a form. But Im unable to get the POST details because it appears that a GET request is being made instead. Code below:

My Form:

Code:
<form id="view-form">
// form details
<input type="hidden" name="form_token" value="<?php echo $form_token; ?>">
</form>

My JS:

Code:
[removed]
$('#view-form').submit(function() {
  $.ajax({
    url: '<?php echo base_url();?>mypage',
    type: 'POST',
    data: {"foo": "123"},
    success: function () {
    console.log('worked');
  }
});
return false;
});
[removed]

My $_SERVER response:

Code:
[REDIRECT_STATUS] => 200
[HTTP_HOST] => localhost:8888
[HTTP_X_REQUESTED_WITH] => XMLHttpRequest
[REQUEST_METHOD] => GET

So the ajax call is working as I get "worked" in the console. BUT I cannot get the post details as a GET request is being created. Why?

I've been all over Stackoverflow and the forums and can't seem to work this out. This is my .hataccess file:

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|beta|js|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

Any ideas what's going on?
#2

[eluser]Otemu[/eluser]
Hi,

Is it a caching issue with the JavaScript?
Can you put the form online so I can test?
Have you tried removing htaccess to locate if that is causing the issue?
Have you checked XMLHttpRequest within the console(firebug/chrome) to see if it says POST or GET??
#3

[eluser]joergy[/eluser]
Your "return false" is outside the commit-handler. Therefor the form is submitted too. And You hadn't specified a method for the form, therefor GET.
#4

[eluser]gregormck[/eluser]
Is it a caching issue with the JavaScript?
- No but any pointers?

Can you put the form online so I can test?
- Sorry not online at the moment

Have you tried removing htaccess to locate if that is causing the issue?
- Yes - same issue

Have you checked XMLHttpRequest within the console(firebug/chrome) to see if it says POST or GET??
- Yeh POSTs to my-page and then does a GET... I have code in my controller where it could potentially redirect however I have commented out and still does POST then GET

Have added POST method to form, so I suspect that's where the POST is coming from, but not able to track down the GET yet. Any ideas?
#5

[eluser]InsiteFX[/eluser]
Because your form open is incorrect.

Code:
<form id="view-form" method="post" accept-charset="utf-8" >

You also need to specify the form action where to post to controller/method.
#6

[eluser]gregormck[/eluser]
Thanks that's working now. I also have PJAX running which was causing a conflict.




Theme © iAndrew 2016 - Forum software by © MyBB