【问题标题】:slim framework and ajax. '/' character as a parameter苗条的框架和ajax。 '/' 字符作为参数
【发布时间】:2016-12-15 03:20:50
【问题描述】:

我正在对 Slim 框架 Web 服务进行 ajax 调用。这是为了将笔记发送到我的数据库。

问题是用户可以写例如“发送 1/2 件”。因此,当我拨打电话时,由于“/”字符,URL 会抛出 404 not found。

有什么办法可以避免这个问题?

notes = 'send 1/2 piece'

$.ajax({
            type: 'GET',
            url: 'http://Myserver/orders/notes/' + MyOrder + '/' + notes,
            dataType: "json", // data type of response
            beforeSend: function(xhr, settings){
            },
            success: function(data){
            },
            error: function(xhr, status, errorThrown){
                errorPopup(errorThrown);                    
            },
            complete: function(xhr, status){
            }
        });

【问题讨论】:

  • EncodeURI / EncodeURIComponent
  • EncodeURIComponent 不适合我。它只是对完整的 URL 进行编码,但我仍然得到 404
  • GET 不是发送任意数据的最佳方法。我会考虑改用 POST。

标签: javascript php ajax slim


【解决方案1】:

您只需要针对笔记运行 EncodeURIComponent。

在您的 ajax 调用之前:

notes = 'We have funny characters in here like /es';
encNotes = EncodeURIComponent(notes);

然后,使用编码字符串创建您的 url 字符串。

url: 'http://Myserver/orders/notes/' + MyOrder + '/' + encNotes,

【讨论】:

猜你喜欢
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 2012-12-04
  • 1970-01-01
相关资源
最近更新 更多