jQuery client won't accept my server responses

2019-09-07 23:16发布

问题:

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!

回答1:

After some days(!) of research I finally found the very simple solution: HTTP access control (CORS).

Long story short - the following code fixed the problem.

I had to take the following steps on my server:

1.) Activate headers module for apache:

server# a2enmod mod_headers

2.) Modify VirtualHost file "myservice.conf" on my server and add the following line between.

Header set Access-Control-Allow-Origin "*"

3.) Check for any errors in your file:

server# apachectl -t

4.) reload and restart apache

server# service apache2 reload && service apache2 restart

5.) Enjoy the server client connection!

Hope I could help anyone who has the same problem!!