douhong6187 2013-12-10 19:13
浏览 69
已采纳

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

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> Your browser sent an invalid request. <\/body><\/html> "}

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
});
  • 写回答

2条回答 默认 最新

  • dougu2240 2013-12-10 19:30
    关注

    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
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)