I have this server side SLIM code:
require 'Slim/Slim.php';
$app = new Slim();
//$app->config('debug', true);
$app->get('/hello', 'hello');
//$app->post('/addConsultant', 'addConsultant');
$app->run();
function hello() {
echo '{"hello": ' . json_encode("merp") . '}';
}
Pretty bare bones right? I mean it is only one single GET.
Now, I have this client side Javascript code:
var rootURL = "http://somabsolutions.se/dev/index.php/";
$('#btnHello').click(function() {
$.ajax({
type: 'GET',
url: rootURL + '/hello',
dataType: "text json",
success: function(data){
alert("Something " + data);
},
error: ajaxFailed
});
return false;
});
function ajaxFailed(XMLHttpRequest, textStatus, errorThrown) {
alert("XMLHttpRequest=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
}
Also pretty easy.
I have this HTML stuff, which holds the button that is bound to the AJAX call by jQuery:
<!DOCTYPE HTML>
<html>
<head>
<title>
Backend test
</title>
</head>
<body>
<form id="testForm">
<button id="btnHello">Hello World</button>
</form>
<script src="javascript/jquery-1.7.2.min.js"></script>
<script src="javascript/main.js"></script>
</body>
</html>
This stuff, used to work, until today, when it for some alien reason stopped doing so!
See, it works, right?
But every time I press that HTML button, Ajax thinks the call fails and redirects me to the error function, which fails to provide me with anything but blank error messages.
What is wrong with this? It worked just fine some days ago!
Okay now I definitively know what was wrong, the problem was that I tried to do the REST call to an URL that was different than from the one was currently on, I was trying to call
http://something.com/argumentfromhttp://www.something.com/argumentApparently, this is enough for JavaScript to think that it is a cross domain call, which it is really not.
There is a parameter in JQuery –
That is supposed to allow cross domain calls, or at least what javaScript thinks to be cross domain calls, but it did nothing for me. I just have to make sure that the REST calls are from the correct URL.