Hi I am facing a strange problem. I need to use webservice from different domain. So I looked into the code of chrome addon Simple REST Client and I have found out, that they are using this function to make XHR
function sendRequest() {
clearFields();
if ($("#url").val() != "") {
var a = new XMLHttpRequest;
a.onreadystatechange = readResponse;
try {
a.open($("input[type=radio]:checked").val(), $("#url").val(), true);
//This code is for adding headers
var b = $("#headers").val();
b = b.split("\n");
for (var c = 0; c < b.length; c++) {
var d = b[c].split(": ");
d[1] && a.setRequestHeader(d[0], d[1])
}
jQuery.inArray($("input[type=radio]:checked").val(), ["post", "put"]) > -1 ? a.send($("#postputdata").val()) : a.send("")
} catch (e) {
console.log(e);
$("#responsePrint").css("display", "")
}
} else {
console.log("no uri");
}
}
So I have created similar one
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var test = $(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "http://aplikace.mvcr.cz/sbirka-zakonu/SearchResult.aspx?q=1/2013&typeLaw=zakon&what=Cislo_zakona_smlouvy", true);
//xmlhttp.setRequestHeader('Access-Control-Allow-Origin','*');
xmlhttp.send();
But with my code I am getting XMLHttpRequest cannot load http://aplikace.mvcr.cz/sbirka-zakonu/SearchResult.aspx?q=1/2013&typeLaw=zakon&what=Cislo_zakona_smlouvy. Origin http://localhost:51582 is not allowed by Access-Control-Allow-Origin.
So where is the problem? Why the former code works and my doesn't even they are almost the same?
EDIT: I have also tried to call hte webservice with this function
$.ajax({
type: "GET",
url: "http://aplikace.mvcr.cz/sbirka-zakonu/SearchResult.aspx?q=1/2013&typeLaw=zakon&what=Cislo_zakona_smlouvy",
contentType: "script; charset=utf-8",
dataType: "jsonp",
success: function (msg) {
var tes = $(msg);
},
error: function (xhr, status, err) {
}
});
and what I want to do is to read the response as DOM, because the webservice returns whole HTML page and I need only one div, but now I get Uncaught SyntaxError: Unexpected token <
which points to this line
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
As I can see in the Network tab, the request works, but due to the error the success function is never called and I cant access the data.