It is known that XmlHttpRequest in Opera only works in the domain where javascript is open. Below I will give a way to avoid this misstep.
We send the data and get the result - picture
To send information to another host instead of XmlHttpRequest, I use a simple DOM that creates a picture that loads the server script from another domain. The result can be obtained image. Here is an example of a script that only sends data to a third-party server:
// test first
function postDataImage (addr) {
// create a new image
var img = document.createElement ("img");
img.src = addr;
// if we want to make the result visible to the user,
// then this picture is connected to the document
document.getElementById ("result"). appendChild (img);
}
// implement the "similarity" of an ajax request in this way
postDataImage ("http://your.domain.com/serverside.image.php");
As a result, we got a picture, for example, it can replace the one that was “clicked”. If our task is only to send data, then the image can not be connected to the document at all. But what about the other results?
')
If the result is a number
As already mentioned above with the pictures in Opera there are no problems, they are requested from any domain. Only the images are not enough for us, give us the numbers (for example, the user ID). With small numbers perhaps it is possible to settle. Take for example the size of the picture we received from the server: width * 256 + height will give us the opportunity to display numbers from 0 to 65535 or from -32767 to 32767. A JPEG-image of this size will not weigh too much.
Here is an example, the script answers Success! when the image received from the server has a width of 290 pixels.
// test second
function postDataImageAndProcess (addr) {
// show the message Loading ...
document.getElementById ("result"). innerHTML = "Loading ...";
// create a picture
var img = document.createElement ("img");
img.src = addr;
// set the trigger when the picture is loaded
img.onload = function () {
// when the image is loaded, check its width
// if 290 pixels, then everything is correct
var result = (this.width == 290)?
"Success, image width 290px!" :
"Other result! Image width is not 290px";
document.getElementById ("result"). innerHTML = result;
}
}
// implement the "similarity" of an ajax request in this way
postDataImageAndProcess ("http://your.domain.com/serverside.image.php");
This, gentlemen, I describe the evolution of engineering, as it matured. And so, I got to the most interesting. How can we make a normal request to the server and get a normal response from it: a text, or an array of data, or something else? I wanted to ask you this question, but by the way, I thought about it and finished the article myself (it turns out to be useful to think;)
Finally, a full request / response
Remember, what other tags have the src parameter? <script>! And we begin to stir up the procedure:
// third test
function postDataScript (addr) {
// show the message Loading ...
document.getElementById ("result"). innerHTML = "Loading ...";
// create an object
var awesome = document.createElement ("script");
awesome.src = addr;
document.getElementsByTagName ("head") [0] .appendChild (awesome);
}
// implement the "similarity" of an ajax request in this way
postDataImageAndProcess ("http://your.domain.com/serverside.javascript.php");
serverside.javascript.php contains
header ('Content-type: text / javascript'); and of course all the attributes so that the results are not cached, and javascript will be registered in the body. For example, I put a line that changes the contents of the result cell in our document:
document.getElementById ("result"). innerHTML = "Yes, it works";
That's all. Most turned out that there is, AJAX.
I clarify that all actions were carried out for Opera 9.61, and the subject of study - cross-domain AJAX requests, which are standardly prohibited in Opera. This topic is far from being exhausted, there is still something to think about:
- Create a library with a set of functions, like MSXMLHTTP, only for cross-domain queries.
- Teach Opera in the same way to make requests with the POST method (is it possible? I think that through forms)
- Offer ...;)
A working example is here:
www.blackcrystal.net/labs/operacrossdomainrequest/test.html
Server test scripts here:
www.blackcrystal.net/labs/operacrossdomainrequest
You can download an example (html-file), open it on localhost and test it. Requests to my server work as a watch. And not only in Opera ;-)