使用简单PHP代理的跨域getJson请求 [英] Cross-domain getJson request with Simple PHP proxy

查看:100
本文介绍了使用简单PHP代理的跨域getJson请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从仅返回JSON而不是JSONP的api中获取数据.每次我尝试以JSONP形式连接数据时,由于我不希望使用JSON,因此无法正常工作.因此,我想我必须使用PHP代理来解决跨域问题,以便我可以解释JSON请求. [Simple PHP Proxy] [1]是我正在尝试使用的一种,但是我想让最基本的功能正常工作真是令人费解.当我在示例页面上键入要查询的URL时,它可以工作.当我这样做时,却没有,有时我会收到"Missing Command"(API告诉我争论太多或太少的方式)或httpcode/jsoncode 404.

Hi I'm trying to get data from an api that ONLY returns JSON rather than JSONP. Every time I try to get the data connecting as JSONP it doesn't work because I am not expecting JSON. So, I guess I have to use a PHP proxy to get past the cross-domain issue so I can interpret the JSON request. [Simple PHP Proxy][1] is one that I'm trying to use but I am having a hell of a time trying to get the most basic functionality to work. When I type the url I want to query into his example on the example page, it works. When I do it, it doesn't, sometimes I get a "Missing Command" (the API's way of telling me that there are too many or too little arguements) or a httpcode/jsoncode 404.

Expensify是我正在查询的API.我首先尝试验证命令.如果您在浏览器中键入此URL,则会得到我想要的结果: https://api .expensify.com?command = Authenticate& partnerName = applicant& partnerPassword = d7c3119c6cdab02d68d9&partnerUserID = expensifytest%40mailinator.com& partnerUserSecret = hire_me

Expensify is the API I am querying. I am first trying the Authenticate command. IF you type this URL in your browser, you get the results I want: https://api.expensify.com?command=Authenticate&partnerName=applicant&partnerPassword=d7c3119c6cdab02d68d9&partnerUserID=expensifytest%40mailinator.com&partnerUserSecret=hire_me

使用简单PHP代理,我的请求URL变为:

Using the Simple PHP Proxy my request URL turns out to be: http://people.rit.edu/~cjs6948/exp/ba-simple-proxy.php?url=https://api.expensify.com?command=Authenticate&partnerName=applicant&partnerPassword=d7c3119c6cdab02d68d9&partnerUserID=expensifytest%40mailinator.com&partnerUserSecret=hire_me

这是我非常简单的jQuery代码:

Here is my very simple jQuery code:

var proxy = 'ba-simple-proxy.php';
url = proxy + '?' + "url=" + "https://api.expensify.com?    command=Authenticate&partnerName=applicant&partnerPassword=d7c3119c6cdab02d68d9&partnerUserID=expensifytest%40mailinator.com&partnerUserSecret=hire_me";
$.getJSON( url, function(data){});

"ba-simple-proxy.php"可在[此处] [3]找到.有没有人有尝试通过代理连接到跨域API的经验?也许甚至这个吗?有更好的主意吗?感谢您的任何帮助,谢谢.

"ba-simple-proxy.php" can be found [here][3]. Does anyone have any experience trying to connect to a cross-domain API via a proxy? Maybe even this one? Have any better ideas? Any help is appreciated, thanks.

推荐答案

您需要对URL进行编码,因为参数"partnerName"将传递给您的ba-simple-proxy.php脚本,而不是api.expensify.com网站.我相信您正在寻找的功能是: encodeURI(uri)

You need to encode the URL as the parameter "partnerName" will be passed to your ba-simple-proxy.php script and not the api.expensify.com website. I do believe the function you're looking for is: encodeURI(uri)

这篇关于使用简单PHP代理的跨域getJson请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆