jQuery-Ajax dont work on production server - php

I 've created a web application, the basic idea was to have one page and then with ajax insert data from php-mysql script. On local Xampp the application works perfect.
On production server some ajax requests work but others dont work.
Chrome inspect element shows error:
POST mysite.com/app/show_staff_profile_ajax.php 500 (Internal Server Error) jquery-2.0.3.js:7845
send jquery-2.0.3.js:7845
jQuery.extend.ajax jquery-2.0.3.js:7301
showProfile ajax.js:466
(anonymous function) ajax.js:150
jQuery.event.dispatch jquery-2.0.3.js:4676
elemData.handle jquery-2.0.3.js:4360
On ajax.js line 150 i got:
$(document).on('click','.show_profile',function(){
var id = $(this).attr("id");
showProfile(id); //line 150
return false;
});
And on line 446:
function showProfile(id){
$.ajax({ //line 446
type:"POST",
url: "show_staff_profile_ajax.php",
data: {id : id},
success: function(data)
{
$("#program_panel").html(data);
}
});
}
On show_staff_profile_ajax.php i am taking data from mysql and echo: html tags with php variables.
I am trying two days to fix that issue. I have read many posts but nothing seems to work

A 500 error means there is a an error in your show_staff_profile_ajax.php file. Try to get more information on that - there is usually an error log file for php apps. Find it and it will probably give you more information about what caused the error.
The only data you are passing in the ajax calls is "id" so I would look at that too. Maybe put a line like this:
console.log('profile id: ' + id);
just before you run showProfile(id) in your click function. There may be some HTML elements with incorrect data-id values. This should display the id in your console just before the error occurs.

As karhikr has said, a 500 error indicates that the error is on your server, not in their browser.
Xampp is a Windows package, Windows is case insensitive and relaxed about write permissions - if moving the site to a Linux environment has broken it then the likelihood is that that's where your problem is - a 500 error is more likely to be a write permission error than a capitalisation one.

Related

jquery .serialize() replacing space with + causing 403 Forbidden Error from LITESPEED server on Ajax call

The following function should serialize form data and send it to be processed on the server.
function postIt() {
var postData = $("#myForm").serialize();
$.ajax({
type: 'POST',
data: postData,
url: 'php/makeTopics.php',
success: function(data) {
/* do stuff*/
}
})
}
However, I have just started receiving 403 Forbidden errors from the server. Upon investigation, I have found that the .serialize() function replaces whitespace with "+" and that if I resend the data without the "+"s I no longer get the error.
What am I doing wrong? Is this a client or a server issue?
More info:
-Using LITESPEED server,
-I have reduced my php code to <?php echo("Hello World!"); ?> and the problem persists as described, so I think it must be something else in the webserver. Also, this is new behaviour - I have made no code changes at either end to trigger it.
-WORKING DATA EXAMPLE: tn=factorystore&tkw1=manufacturers&tkw2=brickandmortar
-NOT WORKING DATA EXAMPLE: tn=factory+store&tkw1=manufacturers&tkw2=brick+and+mortar
(Note: the above data examples are the 'source' Form Data taken from the Chrome console)
when you say,
reduced my php code to and the problem persists
do you mean, https://yourdomain.com/hello.php return 403?
Generally, You can ask your host to check the server error log to find out why 403 error. 403 could be caused by many reasons. Such as mod_security, or some restriction on some URLs, folders, etc.

AJAX POST 500 Internal Server Error on hosting

I'm develop something that need some data to load on AJAX (using POST method).
When I'm build the code on localhost (using XAMPP on Windows 7). It works pretty well.
But, when I'm moved the code to hosting server, It give me a 500 Internal Server Error..
Here's the code that send the AJAX Request
$.post("the-link-to-ajax-handler",
{
//some parameter
roomid : roomid,
amenities : amenities,
},
function(data){
//process that I do when ajax complete
}
And here's the AJAX Handler (using PHP)
$roomid = mysqli_escape_string($conn, $_POST['roomid']);
$amenities = mysqli_escape_string($conn, $_POST['amenities']);
echo $roomid."<br/>".$amenities;die();
*I'm trying to print the parameter but no luck, I'm still get the 500 Error
Screenshot of Network Panel from process on hosting
And here's the Screenshot Network Panel from process on localhost (definitely with the same code)
1) Are you missing any javascript plugin files or the plugin files path is not defined you faced the 500 internal server error.
2) Hosting server is Case Sensitive check the Variable values.
3) If you move to ajax before alerting the flow.
Read your error logs to find out problem or try adding error_reporting(E_ALL) to your code then run again.
Try this :
$.ajax({
url: "the-link-to-ajax-handle", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: {roomid:roomid,amenities:amenities}, // Data sent to server, a set of key/value pairs representing form fields and values
dataType: 'json'
}).done(function (data) {
console.log(data);
}).fail(function (data) {
console.log('failed');
});
I had same issue, i noticed that my php files permission was set to 0755. i changed it to 0644 and it worked well.

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)
{
}
});

How to diagnose AJAX error which is not throwing any error messages?

I have following part of ajax code:
$(document).ready(function(){
$("form input#dodaj").click(function(){
var s = $("form input#zad").val();
var str = "<li>"+s+"</li>";
$.ajax( {
type: "GET",
url: "http://lesni.org/kss/dodaj_zadanie.php",
data: {
pid: ($(this).attr('alt')),
zad: encodeURI(s)},
error: function( err ){ alert(err); }
}
).done(function(){
$("ul#zadania").append(str);
$("form input#zad").val(" ");
});
});
});
Note: The page loading the code above is from a URL like http://lesni.org/some/page, and so the Same Origin Policy isn't an issue.
But it does not seem to work and I don't know why.
The PHP file it is calling is correct. When I type manually in browser:
http://lesni.org/kss/dodaj_zadanie.php?pid=1&zad=abc
it works correctly (adds record to database). But the ajax code is not working. And it throws no error or I don't know if it throws any error...
So the question is: how can I diagnose this part of AJAX code to know what's wrong. I also tried the POST version, with no effect.
You're specifying data like this:
data: {
pid: ($(this).attr('alt')),
zad: encodeURI(s)
}
When you pass an object to jQuery via data, you don't need (or want) to encode it. (You only do that with a string.) jQuery does that for you. (If you did want to encode it, you'd want encodeURIComponent, not encodeURI). So you'll end up with entities that are double-encoded, and thus won't work corerctly.
That should be:
data: {
pid: ($(this).attr('alt')),
zad: s
}
(You also don't need the parens around the value of the pid, but they're harmless so I've left them.)
But answering the question
How to diagnose AJAX error which is not throwing any error messages?
...you do that by
Looking at the Network tab in any decent browser's development tools, which shows you exactly what it sent to the server and what it got back.
Using server-side debugging (either proper debugging, or by dumping out things to a log) to look at exactly what got received by the server.
Looking at server-side logs to ensure that the correct resource got requested.
...and so on.

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