我想打一个Ajax请求 ChemSpider网络阿比从Web应用程序,我敢让 - 例如,使用 GetCompoundInfo 搜索功能。
I want to make an Ajax request to the ChemSpider Web Api from a web application that I'm making -- for example, using the GetCompoundInfo search function.
这个API返回的XML格式的数据。例如,一个搜索吡啶
This API returns its data in XML format. For example, a search for pyridine:
GetCompoundInfo?CSID=1020&token=redacted-security-token
结果
<?xml version="1.0" encoding="utf-8"?>
<CompoundInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.chemspider.com/">
<CSID>1020</CSID>
<InChI>InChI=1S/C5H5N/c1-2-4-6-5-3-1/h1-5H</InChI>
<InChIKey>JUJWROOIHBZHMG-UHFFFAOYSA-N</InChIKey>
<SMILES>C1=CC=NC=C1</SMILES>
</CompoundInfo>
好象很简单。这是我的Ajax请求:
Seemed simple enough. This was my Ajax request:
$.ajax({
crossDomain: true,
type: 'GET',
url: "http://www.chemspider.com/Search.asmx/GetCompoundInfo",
data: {
"CSID": 1020,
"token": "redacted-security-token",
},
success: function(xmlstring, st, x) {
success_stuff(xmlstring);
},
failure: function(xmlstring, st, x) {
failure_stuff(xmlstring);
}
});
但是,服务器似乎不具有跨域,资源共享功能。
However, the server doesn't seem to have Cross-Origin-Resource-Sharing enabled.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.chemspider.com/Search.asmx/CompoundInfo?CSID=1020&token=redacted-security-token. This can be fixed by moving the resource to the same domain or enabling CORS.
我不能使用JSONP,因为数据以XML格式返回,而不是JSON。并设置跨域=真似乎并不奏效。
I can't use JSONP, because the data is returned as XML, not JSON. And setting crossDomain = true doesn't seem to be working.
我如何做一个简单的GET请求到该网站的API?
How can I make a simple GET request to this web API?
推荐答案
首先,获得与ChemSpider开发团队联系。问他们,如果他们想打开API弥补CORS请求。他们可能会很乐意提供帮助。
First, get in touch with the ChemSpider development team. Ask them if they'd open the API up for CORS requests. They might be happy to help.
如果这不是一种选择,你需要设置代理,直接进行调用作为一个GET请求,而不是一个Ajax请求。然后,让你的Ajax请求命中代理,而不是API的域。
If that isn't an option, you'll need to set up a proxy which makes the calls directly as a GET request rather than an ajax request. Then make your ajax requests hit the proxy instead of the API's domain.
如果你是一个Python程序员,uwsgi和放大器;的urllib使这个pretty的简单。将下面的code为 ajaxproxy.py
If you're a python programmer, uwsgi & urllib make this pretty simple. Save the following code as ajaxproxy.py.
请注意以下几点code是仅用于演示如何工作,不解决安全问题。它有效地运行一个开放的代理。除非你想各种有趣的业务托管于该服务器上的的名义进行的,不要以此作为-是用于生产。的
"""
Run with:
uwsgi --http :9090 --wsgi-file ajaxproxy.py
"""
import urllib2
def application(env, start_response):
# Get the resource url from the proxy's url & query string.
resource_base_url = env['PATH_INFO'].split('/getresource/')[-1]
if resource_query == '':
resource_url = resource_base_url
else:
resource_url = '{}?{}'.format(resource_base_url, resource_query)
# Get the resource.
print('Getting resource: {}'.format(resource_url))
resource_response = urllib2.urlopen(resource_url)
resource_content = resource_response.read()
# Return the resource to the requester.
start_response('200 OK', [('Content-Type','text/html'), ('Access-Control-Allow-Origin', '*')])
return [resource_content]
uwsgi可以容易地与安装
uwsgi can be easily installed with
pip install wsgi
所有这一切将在你的Ajax请求改变将是网址:
All that would change in your ajax request would be the url:
$.ajax({
url: 'http://127.0.0.1:9090/getresource/http://www.chemspider.com/Search.asmx/GetCompoundInfo',
...
});
课程除了开发及放任何东西;测试你需要运行一个公开可用的服务器上安装代理,改变127.0.0.1到其地址。
Of course for anything besides development & testing you'll need to run the proxy on a publicly-available server and change 127.0.0.1 to its address.
相关推荐
最新文章