Ajax调用使用PHP代理失败的20%来检索JSON [英] ajax call to retrieve JSON using php proxy fails at %20
问题描述
我有一个小问题。我送一个AJAX调用使用托管在我的网站上的PHP代理来检索JSON。它的工作很好,但只要有一个空间的参数,它会连接codeD为20%,而我得到的:
{地位:{HTTP_ code:400},内容:< HTML><身体GT;< H1> 400错误请求< \ / H1&GT ; \ n您的浏览器发送一个无效的请求\ñ< \ /身体GT;< \ / HTML> \ñ\ N}
如何,我还能连接code中的空间,使这项工作?下面是错误的请求URL的一个例子。空间出现在城市名称圣莫尼卡:
<$c$c>http://www.mysite.com/ba-simple-proxy.php?url=https%3A%2F%2Fwww.eventbrite.com%2Fjson%2Fevent_search%3Fcity%3DSanta%20Monica%26region%3DCA%26country%3DUS%26within%3D15%26within_unit%3DM%26start%3D2013-12-10%26max%3D50$c$c>
编辑:AJAX请求在这里:
$。阿贾克斯({
键入:GET,
url:'http://www.mysite.com/ba-simple-proxy.php?url=https%3A%2F%2Fwww.eventbrite.com%2Fjson%2Fevent_search%3Fcity%3DSanta%20Monica%26region%3DCA%26country%3DUS%26within%3D15%26within_unit%3DM%26start%3D2013-12-10%26max%3D50',
数据类型:JSON,
成功:jsonFunction,
错误:jsonFunction
});
不要试图连接code中的数据在自己的网址。让jQuery的为您代劳。
VAR搜索= {
城市:圣莫尼卡',
区域:CA,
国家:美国,
内:15,
within_unit:'M',
开始:2013年12月10号,
马克斯:50
};
VAR URL ='https://www.eventbrite.com/json/event_search?'+$.param(search);
$阿贾克斯({
键入:GET,
网址:HTTP://www.mysite.com/ba-simple-proxy.php,
数据: {
URL:
},
数据类型:JSON,
成功:jsonFunction,
错误:jsonFunction
});
I have a small problem. I'm sending an AJAX call to retrieve JSON using a php proxy hosted on my website. It's working great, but whenever there's an argument with a space, it gets encoded as %20 and I get back:
{"status":{"http_code":400},"contents":"<html><body><h1>400 Bad request<\/h1>\nYour browser sent an invalid request.\n<\/body><\/html>\n\n"}
How else can I encode the space to make this work? Here's an example of the bad request URL. The space appears in the city name "Santa Monica":
http://www.mysite.com/ba-simple-proxy.php?url=https%3A%2F%2Fwww.eventbrite.com%2Fjson%2Fevent_search%3Fcity%3DSanta%20Monica%26region%3DCA%26country%3DUS%26within%3D15%26within_unit%3DM%26start%3D2013-12-10%26max%3D50
EDIT: AJAX request here:
$.ajax({
type: 'GET',
url:'http://www.mysite.com/ba-simple-proxy.php?url=https%3A%2F%2Fwww.eventbrite.com%2Fjson%2Fevent_search%3Fcity%3DSanta%20Monica%26region%3DCA%26country%3DUS%26within%3D15%26within_unit%3DM%26start%3D2013-12-10%26max%3D50',
dataType: "json",
success: jsonFunction,
error: jsonFunction
});
Don't try to encode the data yourself in the URL. Let jQuery do it for you.
var search = {
city: 'Santa Monica',
region: 'CA',
country: 'US',
within: 15,
within_unit: 'M',
start: '2013-12-10',
max: 50
};
var url = 'https://www.eventbrite.com/json/event_search?'+$.param(search);
$.ajax({
type: 'GET',
url:'http://www.mysite.com/ba-simple-proxy.php',
data: {
url: url
},
dataType: "json",
success: jsonFunction,
error: jsonFunction
});
这篇关于Ajax调用使用PHP代理失败的20%来检索JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!