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

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?

Related

PHP exec() command works flawlessly in code, but not when called by AJAX

This command works great if I call it during page execution
exec('run.exe -something', $response, $status);
But I have a button on the page that executes the same command via AJAX:
// Attempt a call to change the password
$.ajax({
url: 'actions/runcommand.php',
type: 'POST',
data: {
cid: cid,
},
dataType: 'json',
success: function(msg)
{
if (ajaxErr(msg) return;
// Show success message
showMsg('success!');
},
error: function(jqXHR, textStatus, errorThrown){ajaxFail(jqXHR, textStatus, errorThrown)}
});
I've verified the two are executing literally the same string whether I do it in my code or via the AJAX, but the AJAX initiated one is returning an error code instead of functioning. The value of status is 1 which is a generic error so I have little to go on.
I looked around and found one similar post, but they solved it by removing https from the ajax command. If I do that the browser blocks it with scary messages.
This is running on a local machine which attempts HTTPS but fails because it's a self-signed cert or something. I turned off https redirection for the site, but that made zero difference. Not sure what it's unhappy about... please help!
if (ajaxErr(msg) return;
Error handling shouldn't be done in the success callback function. You can remove this or modify what this function is doing.
If you're requesting the URL from a different directory, make sure your URL parameter uses a leading slash for the root directory:
url: '/actions/runcommand.php',
Try this and see if there's still a generic error:
$.ajax({
url: '/actions/runcommand.php',
type: 'POST',
data: {
cid: cid,
},
dataType: 'json',
success: function(data, status) {
console.log(data);
console.log(status);
showMsg('success!');
},
error: function(jqXHR, textStatus, errorThrown) {
ajaxFail(jqXHR, textStatus, errorThrown)
}
});
Ok folks, this had all the signs of a boneheaded mistake and that's what it was. I'm leaving it here for posterity because it's an easy mistake to make and I saw other people ask similar questions with no good answer.
The SOLUTION was that my AJAX files were in a SUBDIRECTORY and that means when they executed the command the output which was trying to export to a subdir of the root was trying to find that subdir in ANOTHER SUBDIR. Sheesh!

jQuery ajax POST treated as GET with Laravel on the server

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/"

$.ajax not a function in yii

I have the below function that I used in two different pages and worked perfectly in both cases. Suddenly my pages stopped updating and I got the error $.ajax not a function.
function update_report(data) {
var request = $.ajax({
url: "report?poids="+data,
type: "GET",
dataType: "html"
});
request.done(function(msg) {
$("#yw2").html(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
req ="a";
}
I then changed the $ to jQuery. Then it started sending AJAX requests but it didnt get the relative URL. Like I was generating the request from a page like /index.php/link1 and my code was sending request to /index.php/report rather then /index.php/link1/report
So, then I changed my url: to "link1/report=?"+data then it started sending request to the right page but couldnt update the response as it didnt find the div #id. Got error
TypeError: $(...) is null
What can cause all this issue and how to fix this? And why $.ajax suddenly stopped working, though I didnt make any changes?
JQuery can be not available by $.ajax(). Check if jQuery.ajax() is available or you included jquery twice.
You should not require jquery manually. The best way to include jquery is using Yii::app()->clientScript->registerCoreScript('jquery'). Path for library is set in config/main.php.

Ajax call giving error 500

I have two folders with a php on my domain. From one file I want to send data to the other via using ajax call. The call worked for me on localhost, but when I had my website on live server it gives 500 error on the same ajax call ?
How can I solve this issue ?
My ajax call is as following:
var data_to_send = {};
j.ajax({
url : '../orangehrm/symfony/web/index.php/auth/login',
type: "POST",
data : data_to_send,
async: false,
success: function(data, textStatus, jqXHR)
{
time_zone = j(data).find('#hdnUserTimeZoneOffset').val();
},
error: function (jqXHR, textStatus, errorThrown)
{
}
});
A 500 number error is a server-side exception - something went wrong in Apache or IIS or whatever. To determine what that was, you can check the error logs on the server (Event Viewer on Windows), or turn on "detailed error messages" in your web service software for remote requests to see more information returned to the browser - browsing locally on the server can have the same effect.
Your ajax call is ok (as a call, cannot account for what you post there).
Error 500 = something went wrong on the server side. It could go from a mistyped function name in a script or something similar to a bad config that kills the script. Activate logging on the server, or activate displaying errors on the server (you can find this easy with a simple google type).
Complete list of codes here: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes (you can see that 500 is a generic error).
Now... some wild guesses: Since you said on your local server went fine, but broke on live, there are some usual suspects... one of them would be file permissions. On your local server you are a god, while on live you are well... less god. If your that php script uploaded files, or write something on disc, check that the user that runs the apache have rights to access/read/write that specific things.
Another usual suspect is the database connection (in case you have any). The credentials on live may be different.
And another one... php version/php extensions/php configs (like short tags for example).
Again... it may be anything... but usually I've seen that people make mistakes covered by those 3.
The 500 http code means your server returns an Internal Error on the accessed url.
Try to access that url without Ajax, then try to alert the url(see code bellow), copy the alerted string and paste it in your browser to see if you are pointing on the wished resource or not. This because your url seem to be poor and pointing to another thing than the wished.
DEBUG CODE:
var data_to_send = {},
_url='../orangehrm/symfony/web/index.php/auth/login';
//console.log(_url);
alert(_url);//copy this alerted _url and paste in the browser
j.ajax({
url : _url,
type: "POST",
data : data_to_send,
async: false,
success: function(data, textStatus, jqXHR)
{
time_zone = j(data).find('#hdnUserTimeZoneOffset').val();
},
error: function (jqXHR, textStatus, errorThrown)
{
}
});

Ajax limiting amount of items returned

I'm making a AJAX request like this:
$("#botao_pesquisar_material").click(function(){
$(this).attr("disabled", "disabled");
$("#material").addClass('loading');
$.ajax({
url: '{{ URL::base() }}/material/lista_ajax/',
dataType: 'json',
cache: false,
error: function(data)
{
console.log(data);
},
success: function(data) {
for(x in data)
{
console.log(data[x]);
}
}
});
My PHP method is ok because when I access it via URL, it returns me the expected JSON and when I call this on localhost, it works perfectly so it isn't about special characters or something else with the data.
When I run this on my server I have this:
GET http://dc.portaldeideias.com.br:8080/sergios/public/material/lista_ajax/?callback=jQuery172007718208665028214_1342725644520&_=1342725649090 500 (Internal Server Error) jquery.js:4
f.support.ajax.f.ajaxTransport.send jquery.js:4
f.extend.ajax jquery.js:4
$.click.$.ajax.url
f.event.dispatch jquery.js:3
f.event.add.h.handle.i
Using console.log(data) I have the entire JSON response but here is what is really strange:
The readyState is 4
The responseText is [{"attributes":{"pk_a030_id":78,"a030_descricao":"Camur\u00e7a"},"original":{"pk_a030_id":78,"a030_descricao":"Camur\u00e7a"},"relationships":[],"exists":true,"includes":[]}]
The statusText is Internal Server Error
So, the requested value is created but I receive a Internal Server Error
Sorry for posting this as an answer, but I don't have the required privileges to add this as a comment for your question.
Have you noticed that the URL in the javascript log http://localhost:8080/sergios/public/material/lista_ajax/ is different than the one provided in your screenshot http://dc.p[...]s.com.br:8080/sergios/public/material/lista_ajax?
Could it be the case that you have two different versions of the lista_ajax PHP method hosted in two different servers (maybe one remote and the other one local), and that's why it works flawless when seeing it from the browser and has bugs when tested with ajax?
It's also important to notice that if you are browsing a website hosted on a remote server, and the ajax is configured to a localhost address, it will do a request for your own machine, not the remote server (javascript runs in the browser, so localhost translates to its own client address).
Maybe this is not the case, but it was worth commenting.

Categories