使用php代理检索JSON的ajax调用在%20失败


ajax call to retrieve JSON using php proxy fails at %20

我有一个小问题。我正在发送一个AJAX调用,以使用我网站上托管的php代理检索JSON。它工作得很好,但每当有一个带有空格的参数时,它就会被编码为%20,然后我会返回:

{"status":{"http_code":400},"contents":"<html><body><h1>400 Bad request<'/h1>'nYour browser sent an invalid request.'n<'/body><'/html>'n'n"}

否则我如何对空间进行编码以使其工作?下面是一个错误请求URL的示例。该空间出现在城市名称"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

编辑:AJAX请求在这里:

$.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
});

不要试图自己在URL中对数据进行编码。让jQuery为您做这件事。

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
});

您可以使用urldecode()函数,它将帮助您进行