jQuery客户端将不接受我的服务器响应 [英] jQuery client won't accept my server responses
问题描述
几天前,我编写了一个带有flask的python网络服务,用于在手机(android应用)和服务器之间发送一些JSON数据.
Some days ago I wrote a python web service with flask to send some JSON-Data between my phone (android app) and my server.
一切正常:我可以使用android应用程序以及程序"cURL"通过GET,POST发送和接收数据.
Everything worked fine: I could send and receive data via GET, POST with my android app and also with the program "cURL".
然后,我决定创建一个简单的Web应用程序作为我的Web服务的客户端,并且斗争开始了.所以我写了我能想到的最简单的客户端和服务器:
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>
我的python响应不会显示在浏览器中,所以我还创建了一个非常基本的PHP服务器,其PHP框架Slim作为虚拟主机运行在Apache2服务器上:
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";
});
因此,当我手动从Firefox或Chrome调用此站点时,我在浏览器中看到了一个简单的"This is test#1"字符串,这是我所期望的
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
但是,当我运行"Client.html"后,我什么也没收到:没有任何警报或其他信息.当我使用Firefox的控制台检查错误或其他内容时,一切似乎都还不错:
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:
请求:
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-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
因此,毕竟,我认为服务器必须出了点问题.诸如响应不可用javascript处理"之类的内容.我想到的原因是:
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:
- 客户端无法与在我的服务器上运行的python(烧瓶)或slim(PHP)一起使用
- 客户端可以与其他Web服务(例如,
http://api.openweathermap.org/data/2.5/weather?q=伦敦/
实际起作用的是
- 服务器与Android的httpRequest正常工作
- 服务器使用cURL或通过浏览器直接调用都可以正常工作
提前谢谢!
推荐答案
经过几天(!)的研究,我终于找到了非常简单的解决方案:HTTP访问控制(CORS).
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.
我必须在服务器上执行以下步骤:
1.)激活apache的标头模块:
1.) Activate headers module for apache:
server# a2enmod mod_headers
2.)修改服务器上的VirtualHost文件"myservice.conf",并在它们之间添加以下行.
2.) Modify VirtualHost file "myservice.conf" on my server and add the following line between.
Header set Access-Control-Allow-Origin "*"
3.)检查文件中是否有错误:
3.) Check for any errors in your file:
server# apachectl -t
4.)重新加载并重新启动apache
4.) reload and restart apache
server# service apache2 reload && service apache2 restart
5.)享受服务器客户端连接!
5.) Enjoy the server client connection!
希望我可以帮助任何遇到相同问题的人!
Hope I could help anyone who has the same problem!!
这篇关于jQuery客户端将不接受我的服务器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!