Some days ago I wrote a python web service with flask to send some JSON-Data between my phone (android app) and my server.
Everything worked fine: I could send and receive data via GET, POST with my android app and also with the program "cURL".
Then I decided to create a simple web application as a client for my web service and the struggle began. So I wrote the most simple client and server I can imagine:
Client.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>TestClient</title>
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js</script>
</head>
<body>
<script type="text/javascript">
$.get("http://SERVER_URL/test/1", function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
</script>
</body>
</html>
My python responses would not be displayed in my browser so I also created a very basic PHP-Server with the PHP-framework Slim running on my Apache2 server as a virtualhost:
index.php:
// Slim import and basic stuff...
// GET route
$app->get('/test/:id', function ($id) {
echo "This is test $id";
});
So when I manually call this site (from Firefox or Chrome) I get a simple "This is test # 1" string displayed in my browser as I expected
BUT as soon as I run my "Client.html" I receive nothing: There is no alert or something. When I use Firefox' console to check for Errors or something, everything seems fine too:
REQUEST:
02:49:03.000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0
Origin: null
Host: 213.165.80.252
Connection: keep-alive
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Accept: */*
RESPONSE:
Response-Header Δ91ms
X-Powered-By: PHP/5.4.4-14+deb7u14
Vary: Accept-Encoding
Server: Apache/2.2.22 (Debian)
Keep-Alive: timeout=5, max=100
Date: Sun, 05 Oct 2014 00:49:04 GMT
Content-Type: text/html
Content-Length: 15
Content-Encoding: gzip
Connection: Keep-Alive
So after all, I think there has to be something wrong with my server. Something like "Responses are not javascript processible". Reasons for my thought are:
- Client won't work with python (flask) OR slim (PHP) running on my server
- Client works fine with other web services e.g. http://api.openweathermap.org/data/2.5/weather?q=London/
What is actually working:
- Server works fine with androids httpRequest
- Server works fine with cURL or direct call via browser
Thank you in advance!