jQuery ajax POST treated as GET with Laravel on the server - php

I'm having a bizarre issue where my POST request is being treated as a GET - this only happens on the LIVE environment and works fine locally. I have the correct POST route set up in laravel.
Would there be a case where jQuery would default to GET on a server environment - I'm currently accessing the site via an IP rather than the domain while the DNS resolves, could this perhaps cause an issue?
Route::post('/ajax/sale/filter', 'SalesController#ajaxFilterOptions');
$.ajax({ url: '/ajax/sale/filter/',
data: {filter: options, sale_id: window.saleId, outlet_type: outletType},
type: 'POST',
cache: false,
dataType: 'JSON',
success: _.bind(function (data) {
console.log(data)
}, this)
});

Your code lines up fine, you may want to check your htaccess file to see if a rewrite is happening (or corresponding rewrite with another httpd), or switch to $.post instead of $.ajax to make sure/force it as a POST request.

I know there is an accepted answer here. But whoever comes this page might be interested in knowing that if you remove the leading slash this will work fine. So in the example given, use url "/ajax/sale/filter" instead of "/ajax/sale/filter/"

Related

Opencart multisite registration strange request

I am having a problem that is really stumping me. In my Opencart installation my account registration page isn't working. It is a multisite and the page works fine on the other site. The problem is with the zones, when it tries to get zones based on the country it throws an error. When I examined it with Firebug I see that it sends an OPTIONS request instead of a GET request like it does when generating a successful request on the other page. Unfortunately this isn't the only problem, I was able to get it to send a GET request by specifying "crossDomain: 'false'" as an argument in the .ajax call and that still doesn't fix the error. The cookies being sent and returned are also different. I'm trying to find the underlying problem or at least something that will fix the issue.
Edit:
I added responded to questions in a comment as it Stackoverflow did allow me any more links.
2nd Edit:
I've found that the way you access the registration page matters. Some links to it don't generate any problems. I'm thinking more and more that this probably has something to do with cookies.
The OPTIONS request is indeed very strange in this case and almost looks like the client side (JS/browser) do not know what request methods could be used on the server.
Anyway, in the template catalog/view/theme/<YOUR_THEME>/template/account/register.tpl find this piece of code (almost end of file):
<script type="text/javascript">
$('select[name=\'country_id\']').bind('change', function() {
$.ajax({
url: 'index.php?route=account/register/country&country_id=' + this.value,
dataType: 'json',
beforeSend: function() {
and before dataType: 'json', add type: 'get', or type: 'post', so You should end up with:
<script type="text/javascript">
$('select[name=\'country_id\']').bind('change', function() {
$.ajax({
url: 'index.php?route=account/register/country&country_id=' + this.value,
type: 'post',
dataType: 'json',
beforeSend: function() {
By this You specify which concrete HTTP request method should be used.

pjax submit form URL redirection

PJAX's documentation states that Github uses $.pjax.submit() in submitting a Gist form. A desired feature of an ajax form submission which Github nicely implements is that the URL redirects from the form's action to a newly created URL (in this case, one containing the newly server-side created gist ID).
For example, from this:
https://gist.github.com/gists // form action
to this:
https://gist.github.com/tim-peterson/5019589 //assume this ID is generated server side
I've gotten this to work similarly on my site (i.e., the page itself redirects to the equivalent of https://gist.github.com/tim-peterson/5019589) but I can't redirect the URL (it stays like https://gist.github.com/gists).
Is this entirely a server-side issue (setting headers?) or is there something in pjax that I'm missing? I'm using a version of pjax I downloaded today so it can't be that i'm using a buggier version of pjax.
Did you find any solution to this?
I had the similar issue.
Basically you need to set X-PJAX-URL property in response header.
It's value should be same as Request URL.
In Laravel, I did it like this...
App::after(function($request, $response)
{
$response->headers->set('X-PJAX-URL', $request->getRequestUri());
});
There may be a more-elegant / proper way to do this with $.pjax.submit(), but this is the solution I've been using without any problems. I serialize the form inputs prior to submission, then POST them to my server. Then check the reply from the server to make sure I don't need to prevent the user from going forward before calling my PJAX to continue.
$("#save").click(function(e) {
e.preventDefault();
dataString = $("#form").serialize();
$.ajax({
type: "POST",
url: $("#form").attr("action"),
data: dataString,
dataType: "json",
success: function(data)
{
// Handle the server response (display errors if necessary)
$.pjax({
timeout: 2000,
url: data.url,
container: '#main-content'
});
}
});
});
It happens if response HTML doesn't contain initial PJAX container

ajax crossdomain post to php kohana 3.2 not working

I have 2 (local) subdomains: kohana.local.com and wordpress.local.com. jQuery plugin (.js) is located on kohana.local.com domain and takes care of rating articles and retrieving rate count on both domains. Controller_Rating extends Controller. Method (action_getrating) has following code (kohana 3.2):
if($this->request->post() && $this->request->is_ajax()){
$this->auto_render = FALSE;
echo "{$_REQUEST['callback']}(".json_encode($data).")";
}
Ajax call:
$.ajax({
type: "POST",
url: "http://kohana.local.com/rating/getrating",
dataType: "jsonp",
data: { some_id: id },
success: function(json){
//do something
}
});
When ajax call gets issued from kohana.local.com, everything works great. If it's issued from wordpress.local.com $this->request->is_ajax() is false, and method is not "post", but "get" somehow. What is the reason for this, and how to make it work? Post is required and is_ajax is good for security and validation.
Thanks in advance.
EDIT:
post to jsonp is not possible, so i can't use this approach. i'll have to try to find the solution in the direction of json
You can simply use json dataType, so you don't need to use callbacks. Just add header Access-Control-Allow-Origin to server which requests are made (kohana.local.com).
All domains are allowed:
Access-Control-Allow-Origin: *
Or specify allowed domain:
Access-Control-Allow-Origin: http://wordpress.local.host
Spec: http://www.w3.org/TR/2008/WD-access-control-20080912/#access-control-allow-origin
Multiple domain solution: Access-Control-Allow-Origin Multiple Origin Domains?

AJAX runs error callback on 403 when it has my data?

This is a major revision of an earlier question because I feel I misunderstood the nature of my problem. My GET request sent via AJAX to a standalone PHP script on my own WAMP server is landing me 403 Forbidden... but I get the output I want. Let me explain:
I have...
Set Full Control permissions all across the board. All of them.
I used both relative and absolute paths, along with complete URLS using both 127.0.0.1 and localhost.
The standalone has header('Access-Control-Allow-Origin: *');
Restarted Apache with redundant <Directory ...> and Allow from all directives in .htaccess and httpd.conf.
Went through 8 pages of this
Read everything I could find on StackOverflow.
Sacrificed a goat and threw a virgin into a volcano.
I can access the file directly by typing its address in the browser and see the output I want. When I run this:
$.ajax({
type: 'GET',
url: 'lib/GetNextTags.php',
data: {
context_code : context_code
},
cache: false,
success: function (data, textStatus, jqXHR) {
//...
},
error: function( event, jqxhr, ajaxSettings, thrownError ) {
alert(event.responseText);
},
dataType: "json"
});
The success callback is not called, but the error callback is. The event object passed as an argument contains the output I've been trying to get in event.responseText. event.readyState is 4. I used json_encode() and encoded everything as UTF-8, so I don't think jQuery had an issue with decoding.
So... Why the 403? Why the error callback?

Jquery ajax error - Requests adding GET var

My ajax function has stopped working all of a sudden.
function get_file_info()
{
$.ajax({
type: "GET",
url: "http://localhost/includes/get_file_info.php",
dataType: "json",
jsonp: false,
jsonpCallback: "callbackName",
success: function(data) {
return data;
}
});
}
I did some debugging and found that the ajax request is going to
http://localhost/includes/get_file_info.php?_=1297356964250
I am just would like to know what it is and can be used for, also how to remove so the ajax request is like below so it works again.
http://localhost/includes/get_file_info.php
Many Thanks
If you add:
cache: true,
to your call it will remove the timestamp which is there to always call a different URL's so the browser doesn't cache the result. This is standard for calls except for datatypes script and jsonp.
As others have stated though, it would be good to change the server side to stop turning away anything with GET's, maybe check if the get is a _ and only numeric, if not then turn it away...
The "_=1297356964250" query string is jQuery's method of preventing the URL being cached and returning an old result. Adding this ensures that you're retrieving a new response every time.
This is not the cause of your request breaking down. It must be from another issue. Have you tried logging your response and seeing what it returns?
success: function(data) {
console.log(data);
}

Categories