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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题