php version - 4.4.4
mysql version - 4.1.22
Hi, my problem is rooted in how blogs handle ajax requests from a database returning html in the form of an image element. So the blog contains reference to a js file on the server which interacts with a php script in the same folder that then returns an image element (eg.
) to the js script that writes the code using innerHTML. Page > javascript (creation of XMLHTTPRequest object or IE equivalent) > php script > javascript > Page. The ajax request works fine when I upload a file to my server with the code in the body tag. Something goes wrong when I try pasting the same code in a Blog (that takes pure html). Basically httpRequest.readyState == 4 and httpRequest.status == 200 returns false intstead of true. Can anyone help with this problem??
13 2465
Can you post your code or a link?
Here's what the javacript code looks like...
(I'm a newbie at this) -
function makeRequest(url) {
-
var httpRequest;
-
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
-
httpRequest = new XMLHttpRequest();
-
var browser = "moz";
-
if (httpRequest.overrideMimeType) {
-
httpRequest.overrideMimeType('text/xml');
-
-
}
-
}
-
else if (window.ActiveXObject) { // IE
-
try {
-
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
var browser="ie";
-
}
-
catch (e) {
-
try {
-
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
var browser="ie";
-
}
-
catch (e) {}
-
}
-
}
-
-
if (!httpRequest) {
-
alert('Giving up :( Cannot create an XMLHTTP instance');
-
return false;
-
}
-
//Set function to handle reponse
-
httpRequest.onreadystatechange = function() { alertContents(httpRequest,browser); };
-
httpRequest.open('GET', url, true);
-
httpRequest.send(null);
-
-
}
-
-
function alertContents(httpRequest,browser) {
-
try{
-
if (httpRequest.readyState == 4) {
-
if (httpRequest.status == 200) {
-
// Put response text (an img element) in div called 'random'
-
var newdiv = document.createElement("div");
-
newdiv.innerHTML = httpRequest.responseText;
-
var container = document.getElementById("africapic_photos");
-
container.appendChild(newdiv);
-
} else {
-
var newdiv = document.createElement("div");
-
newdiv.innerHTML = "There was a problem with the request.";
-
var container = document.getElementById("africapic_photos");
-
container.appendChild(newdiv);
-
-
}
-
}
-
}
-
catch( e ) {
-
alert('Caught Exception: ' + e.description);
-
}
-
}
-
-
if (document.getElementById('africapic_hl')){
-
makeRequest('weblinks.php?wli=2997&size=s');}
-
else {
-
var newdiv = document.createElement("div");
-
newdiv.innerHTML = "Please use the code provided.";
-
var container = document.getElementById("random");
-
container.appendChild(newdiv);}
-
The output on a certain blog is "There was a problem with the request"...
Thanks
The code above is one of many automatically generated scripts (each passes diff values to weblinks.php. An example of the corresponding html script follows.... -
<div id="africapic_photos">
-
<a href="http://www.africapic.com/Tranquil-pictures-2997.html" rel="nofollow" id="africapic_hl"> <script src="http://www.africapic.com/wl_js/2997.js" language="javascript"></script></a>
-
</div>
-
The output on a certain blog is "There was a problem with the request"...
So obviously the status is not 200. Have you checked what the status is?
It was 404. I then changed line 60 to an absolute path and now the code doesn't even reach -
function alertContents(httpRequest) {
-
try{
-
alert(httpRequest.readyState);
-
if (httpRequest.readyState == 4) {
-
alert(httpRequest.status);
-
if (httpRequest.status == 200) {
-
// Put response text (an img element) in div called 'random'
-
var newdiv = document.createElement("div");
-
newdiv.innerHTML = httpRequest.responseText;
-
...
-
(there are no alerts). The code works fine on my server but not on a free blog.
You've really got me thinking here.
Please can anyone help? PLeeease
Try accessing weblinks.php with the correct arguments without using Ajax. Does it work?
Yup that works. I put the image src attribute as the weblinks.php script and altered the php so that it returns an actual image not just text. The reason that I've used ajax is that if you look at post 3 line 59 there's a javascript DOM thing that's vital (it corresponds with the id on post 4 line 2) . Unless there's some way you know of to access the DOM using PHP... even that might still not work...
I did the above (post 10) just as a test to see that weblinks.php works fine. In order to send back an image I put a header [php]header("Content-Type: image/jpeg");[/php] Do I need to give a header for outputting text (html)? ie for this (line 6):
[php]
(code to open db connection goes here)
$query="SELECT filename,width,height,category,medianame FROM weblinks WHERE idn=".$_GET['wli']." AND size='".$_GET['size']."'";
$rawdb=mysql_query($query);
$array=mysql_fetch_array($rawdb);
if (!empty($array["filename"])){
echo "<img src='http://www.africapic.com/WebLinks/".$array['filename'].".jpg' width='".$array['width']."' height='".$array['height']."' alt='".$array['medianame']." pictures, ".$array['category']." stock photography' title='".$array['medianame']." pictures, ".$array['category']." stock photography' border='0' />";
}else{
echo "Image not retirievable";
}
mysql_close($dbh);
[/php]
Do I need to give a header for outputting text (html)?
No. The problem is with not being able to access the file because you're getting a status of 404 Page not found.
Check that the relative path matches. Is it in the same directory?
I changed the relative path to an absolute path (post 6) and the 404 disappears. However, I then get nothing (no 404 no 200 not even a readystate). Do blogs by any chance kinda block ajax requests? I'll try the code on another blog quick.
Do blogs by any chance kinda block ajax requests?
They shouldn't do. You did get a readyState of 4 before, so something's right.
I think the problem is with it not finding the PHP file.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lyle Fairfield |
last post by:
In a recent thread there has been discussion about Data Access Pages. It
has been suggested that they are not permitted on many or most secure sites.
Perhaps, that it is so, although I know of no...
|
by: geevaa |
last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3
XMLHttpRequest and AJAX for PHP programmers
James Kassemi
Introduction:
Although the concept isn't entirely new, XMLHttpRequest...
|
by: schmeckel |
last post by:
I am trying to use some very basic AJAX functionality (update panel
for partial page update) on my website. When I run my webpage in VS
2005, the partial page update works fine. However, when I...
|
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= |
last post by:
controlsPlease could some of you here post some of your live examples of AJAX
(esp drag panels, collapsable panels, and popup menu.)
(It's one thing to talk about how great something is, but it's...
|
by: shivendravikramsingh |
last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
|
by: Robert Cramer |
last post by:
Just wondering if any of you actually implement portal
sites using ASP.NET Web Parts, or use Web Parts outside of portal-style
sites.
I'm just now learning about Web Parts, and was wondering if...
|
by: raknin |
last post by:
I'm using AJAX on my website, but internet explorer does not seem to actually be refreshing the data I retrieve via AJAX when I refresh the page. For example, I have a button that when pressed uses...
|
by: sheldonlg |
last post by:
I am still improving my AJAX and I ran into something that I'm not sure
how to do.
I will have a page called where I will have a dropdown list at the top
and a large display at the bottom. The...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |