I am trying to make a simple AJAX calling in PHP and have a problem with Internal error 500. When I click on the link, so the data are successfully loaded - this I see through FireBug, but I got the error above. It's on localhost.
When I try to set to the URL of a browser the address, that is called by AJAX, so the content is successfully loaded.
Where I should start to search the problem - or what could be wrong?
EDIT: in apache log is nothing weird, looks fine.
If after checking your php error log you don't find any issues, could it be your javascript AJAX call expects results to be returned in a specific format like JSON?
If this is the case, you need PHP to set the correct content type header when it is responding to the AJAX call, like so:
header('Content-type: application/json');
So in context, this might look something like:
$some_data = array(
'user_id' => 47,
'first_name' => 'Mike',
);
header('Content-type: application/json');
echo json_encode($some_data);
Look in the PHP error logs, or perhaps even your web server's error log.
Or if your script spews out errors to the client (which it hopefully doesn't do, at least not in production), try taking a look at the response in Firebug.
You most likely have a bug in your PHP. It doesn't matter if it is an AJAX call. Look at your server logs.
Related
We have a contact.html form that uses reCaptcha v2, whose backend processing is in a php file.
I've taken enough steps to understand that when we send the verification to google's api, the response comes back empty. Below is code that gave me this proof.
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST["g-recaptcha-response"].'&remoteip='.$_SERVER['REMOTE_ADDR'];
$verify = file_get_contents($url);
echo $url;
if (empty($verify)) echo 'Failed to fetch data';`
However, when I manually enter the url into a browser, I get a JSON response back that indicates success.
What, then, is the difference? Why would file_get_contents return empty if a simple get request from a Chrome browser give me trouble?
I have read that file_get_contents is synchronous, so I wouldn't expect this is just a noob error on waiting for the response.
Any help would be appreciated, this is my very first time working with PHP. It's not hard, but I may be missing something vital.
Sorry everyone, I can't understand why, but the problem was in the method used to access the site verify.
Using curl syntax, I finally got it working.
Change the configuration in php.ini file and don't need curl.
allow_url_fopen=0 to allow_url_fopen=1
I'm creating a global high score table. The JavaScript sends the username and score to the PHP through some AJAX functions and POST. The PHP script then takes the variable, and compares the score values to the file on the server. It then puts the new score where it should go on the score table, and rewrites the file.
I had it working last night, but now, when I call the function, it displays my code instead, and firebug gives the error, "No element found". I'm not sure what I did between then and now, but I just can't figure this one out.
This is my first time using PHP and AJAX, so I'm not sure where or what the error is.
Any help would be appreciated!
The exact error:
Timestamp: 4/13/2013 1:59:18 PM
Error: no element found
Source File: file:///C:/wamp/www/ajax.php
Line: 84, Column: 3
Source Code:
?>
If I cannot help you solve this issue, I will delete this answer, but I wanted to post it like this so you can better read my suggestions.
Start by changing your HandleResponse function to this:
function HandleResponse(response)
{
console.log(response);
document.getElementById('ResponseDiv').innerHTML = response;
}
and post the result that appears above the error, in firebug.
What we are doing here is adding a way for us to see if any data was actually returned from the AJAX request. A call to console.log tells the browser to print the given argument to the console (in your case, firebug).
UPDATE 1
In your comments, you stated that nothing displayed when you logged the value of response. This means that you received nothing back from the server (in terms of data, at least).
My next suggestion is that you change the call to MakeRequest to the following:
<input type='button' id="test" onclick='MakeRequest("*");' value='Global Highscore Table'/>
Like before, leave the console.log line in the HandleResponse method, and post the result that appears above the error in firebug.
Here, we are setting your MakeRequest to make a request that passes a wild-card query parameter. At the time of this suggestion, I did not realize that the requested file expected two arguments, nor that passing a wild-card would error the script. However, this was still a good thing to do, as we found a new error, which tells us that the request is being received.
UPDATE 2
Now, cut and paste everything from your "ajax.php" file to your notepad or another, similar application. Then, set the following to be the only content of the "ajax.php" file:
<?php echo "Request received and response sent"; ?>
Again, post the result of the console.log.
Here, we are taking a step back from fixing the complex code, and we are going back to the basics. All we want to do, here, is verify that we can both send the request and receive a response.
If "Request received and response sent" is successfully returned as the response of the request, then we know that the error lies in ajax.php, and not in the request. If it is not, we know that there is a problem with the request (be it a server issue or the request, itself). If the latter is the case, there could still be a problem with ajax.php, but we must first fix the request.
UPDATE 3 (final solution)
Issue was found to be due to not correctly running on the local server.
I am quite frustrated with a very weird issue. I am trying to do a very simple thing here. I would like to post a form to an endpoint. I tried using the following command:
curl -d "Contact=Aditya&address1=1510 E. 9th Street&address2=Apt. 111&city=Tucson&state=AZ&zip=85719&Phone1=5207849817&Phone2=1237849812&email=aditya15417#yahoo.com&key2=09 New Lead&key5=AmpushU&uhsgradyr=1950&uhighlevel=AA&ucourseint=BA Internet Mktg&uCampaignID=Herlambang&utextperm=YES&uleaddate=20110910&uleadtime=19:18 PST" http://dev.degreeamerica.com/candidate_test.php
And yes it succeeds just fine, however when I try it via the site it gives me an error. Can someone tell me what I did wrong? The parameter I have on the curl is just something I copy and paste from firebug POST request, so the parameter is all the same.
Is there something wrong with me doing the AJAX?
You can't receive the results of a cross-domain post like that. Your page is on adityaherlambang.com, but you're posting to degreeamerica.com, so you can't get the results
One solution is to use a server-side proxy on adityaherlambang.com, and post using libcurl to degreeamerica.com, while returning whatever kind of status you want to your own site's JavaScript.
I've been messing around with SWF Upload, and I really like it. I was able to apply it very easily to my file upload class with very little issue. The problem I am having, however, is that I need to be able to send back errors to the uploader so a user knows if their file actually wasn't completely uploaded due to issues such as it being too large, or of the wrong format, ect. I read through the PHP example script that the writers of SWF Upload provide, but I cannot figure out how to pass back the errors to it. This is there error handling function:
/* Handles the error output. This function was written for SWFUpload for Flash Player 8 which
cannot return data to the server, so it just returns a 500 error. For Flash Player 9 you will
want to change this to return the server data you want to indicate an error and then use SWFUpload's
uploadSuccess to check the server_data for your error indicator. */
function HandleError($message) {
header("HTTP/1.1 500 Internal Server Error");
echo $message;
}
So the uploadSuccess function is expecting a response from the server. How would I go about encoding and sending that said response using "header()"?
If I use this:
header("HTTP/1.1 200 Uploaded File was Successful");
SWF Upload returns success, but that message does not come through to the uploadSuccess function.
I was also looking for a way to display an error message from the server. Based on the comments above, I was able to get this to work by updating the uploadSuccess() function in handlers.js to:
if (serverData != null && serverData != "ok"){
progress.setError();
progress.setStatus(serverData);
} else {
progress.setStatus("Complete.");
}
Meaning, if the response is anything other than "ok" it will be displayed as an error. This feels pretty hacky. I'd much rather be able to send a non-200 response from the server and have it show the error message from that, but as far I can tell that's not possible. Also, the way I've got it set up wouldn't work for custom messages that aren't errors.
I'am building simple Ajax application (via jquery). I have strange issue. I found where the problem is, but I don't know how to solve it.
This is simple server-side php code:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
On the client side, the error "Invalid JSON" is thrown.
I have discovered that if I delete require function, everything work fine.
Just for information, the "some.php" is an empty php file. There is no error when I open direct php files.
So, conclusion: I cant use require or include function if I want to use ajax?
Use Firebug to see what you're actually getting back during the AJAX call. My guess is that there's a PHP error somewhere, so you're getting more than just JSON back from the call (Firebug will show you that). As for your conclusion: using include/require by itself has absolutely no effect on the AJAX call (assuming there are no errors).
Try changing:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
To:
<?php
$return = array(
'pageContent' => 'test'
);
echo json_encode($return);
?>
The problem might have to do with $return not being declared as an array prior to use.
Edit: Alright, so that might not be the problem at all. But! What might be happening is you might be echoing out something in the response. For example, if you have an error echoing out prior to the JSON, you'd be unable to parse it on the client.
if the "some.php" is an empty php file, why do you require it at all?!
require function throws a fatal error if it could't require the file. try using include function instead and see what happens, if it works then you probably have a problem with require 'some.php';
A require call won't have any effect. You need to check your returned output in Firebug. If using Chrome there is a plugin called Simple REST Client. https://chrome.google.com/extensions/detail/fhjcajmcbmldlhcimfajhfbgofnpcjmb through which you can quickly query for stuff.
Also, it's always good to send back proper HTTP headers with your response showing the response type.
It's most likely the BOM as has been discussed above. I had the same problem multiple times and used Fiddler to check the file in hex and noticed an extra 3 bytes that didn't exist in a prior backup and in new files I created. Somehow my original files were getting modified by Textpad (both in Windows). Although when I created them in Notepad++ I was fine.
So make sure that you have your encoding and codepages set up properly when you create, edit, and save your files in addition to keeping that consistent across OSes if you're developing on windows let's say and publishing to a linux environment at your hosting provider.