On http://83.150.87.220/HelsinkiViSe/helsinki-map-application/ajax_test.php you can send a very simple async HTTP request when pushing the button on top of the page.
Bellow is the code for it:
var xmlString = "<request><session></session><target_id>20</target_id></request>";
// Build the URL to connect to
var url = "http://83.150.87.220/HelsinkiViSe.dll/load";
$.ajax({
type: "POST",
url: url,
dataType: 'application/xml',
data: xmlString,
success: function(msg) {
//var data = JSON.parse(msg);
$("#text").html(msg);
},
error: function(jqXHR, textStatus, errorThrown) {
$("#text").html(jqXHR+'<br>'+textStatus+'<br>'+errorThrown);
}
});
As you can see, the request data block is a simple piece of XML:
var xmlString = "<request><session></session><target_id>20</target_id></request>";
The back-end script is supposed to respond with an XML block as well, which it does (monitoring communication using Firebug and Chrome’s “code inspector”). This block is:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<target>
<target_id>20</target_id>
<plot>20803/3</plot>
<builder>(makasiini L3)</builder>
<housing_form>avoin</housing_form>
<block_name>JätkäHieta</block_name>
<finnish_year></finnish_year>
<target_name></target_name>
<target_address></target_address>
<office_space></office_space>
<purpose></purpose>
<reservations></reservations>
<contacts></contacts>
</target>
<status>OK</status>
<errormsg></errormsg>
</response>
The problem is that the error handler launches nevertheless, and the success handler doesn’t. And this is giving me white hairs. Do you know what might the cause for this be and how to fix it?
Thanks in advance.
Andrei
There are at least two and possibly three problems, one of which I’m sure is just debug code:
You’ve told jQuery to use
dataType: "application/xml". ThedataTypeargument isn’t a MIME type, it’s a jQuery-specific thing. You wantdataType: "xml".(This is probably the debug thing.) In your
successfunction, you’re passing the XML document into thehtmlfunction, which will raise an error. If you change it totextinstead, it’ll show"[object Document]"instead, which makes sense — jQuery has deserialized the XML into an XMLDocumentobject for you.You’re sending the XML string without encoding it. When you specify a string for the
dataargument, you are responsible for ensuring it’s properly encoded for transit (reference). I’m fairly sure you need to change the line…to
I say “fairly sure” because I don’t remember the last time I tried to post XML to a server. But anything you’re sending via
POSTshould typically be URL-encoded.Live working example | source