URL在jQuery中为AJAX请求编码字符串

Bri*_*n D 211 javascript ajax jquery http

我正在我的应用程序中实现Google的即时搜索.我想在用户输入文本输入时触发HTTP请求.我遇到的唯一问题是,当用户到达名字和姓氏之间的空格时,空间不会被编码为a +,从而破坏搜索.如何用a替换空格+,或者只是安全地对字符串进行编码?

$("#search").keypress(function(){       
    var query = "{% url accounts.views.instasearch  %}?q=" + $('#tags').val();
    var options = {};
    $("#results").html(ajax_load).load(query);
});
Run Code Online (Sandbox Code Playgroud)

And*_*tad 480

尝试encodeURIComponent.

通过将某些字符的每个实例替换为表示字符的UTF-8编码的一个,两个,三个或四个转义序列来编码统一资源标识符(URI)组件(对于由两个"代理"组成的字符,将只有四个转义序列"人物".

例:

var encoded = encodeURIComponent(str);
Run Code Online (Sandbox Code Playgroud)

  • 这解决了我的问题 - 当我将URL作为参数传递给我的AJAX请求时,URL在&之后丢失了所有查询字符串.当我添加这个,这解决了我的问题.谢谢! (2认同)

Pra*_*n04 21

encodeURIComponent对我来说很好.我们可以在ajax调用中给出这样的url.代码如下所示:

  $.ajax({
    cache: false,
    type: "POST",
    url: "http://atandra.mivamerchantdev.com//mm5/json.mvc?Store_Code=ATA&Function=Module&Module_Code=thub_connector&Module_Function=THUB_Request",
    data: "strChannelName=" + $('#txtupdstorename').val() + "&ServiceUrl=" + encodeURIComponent($('#txtupdserviceurl').val()),
    dataType: "HTML",
    success: function (data) {
    },
    error: function (xhr, ajaxOptions, thrownError) {
    }
  });
Run Code Online (Sandbox Code Playgroud)


Sta*_*low 9

更好的方法:

encodeURIComponent转义除以下内容之外的所有字符:alphabetic, decimal digits, - _ . ! ~ * ' ( )

为了避免对服务器的意外请求,您应该对任何用户输入的参数调用encodeURIComponent,这些参数将作为URI的一部分传递.例如,用户可以为变量注释键入"Thyme&time = again".不对此变量使用encodeURIComponent将再次给出comment = Thyme%20&time =.请注意,&符号和等号表示新的键和值对.因此,不是让POST注释键等于"Thyme&time = again",而是有两个POST键,一个等于"Thyme",另一个(时间)等于.

对于application/x-www-form-urlencoded(POST),根据http://www.w3.org/TR/html401/interac...m-content-type,空格将替换为'+',所以可能希望跟随encodeURIComponent替换,并使用"+"替换"%20".

如果希望更加严格遵守RFC 3986(保留!,',(,)和*),即使这些字符没有正式的URI分隔用法,也可以安全地使用以下内容:

function fixedEncodeURIComponent (str) {
  return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
}
Run Code Online (Sandbox Code Playgroud)


d1j*_*i1b 6

我使用 MVC3/EntityFramework 作为后端,前端通过 jquery 使用我所有的项目控制器,当您直接传递除 URL 硬编码以外的参数时,直接发布(使用 $.post)不需要数据加密。我已经测试了几个字符,我什至发送了一个 URL(这个http://www.ihackforfun.eu/index.php?title=update-on-url-crazy&more=1&c=1&tb=1&pb=1)作为参数并且有即使当您在 URL 中传递所有数据(硬编码)时 encodeURIComponent 工作得很好,也完全没有问题

硬编码的 URL ie>

 var encodedName = encodeURIComponent(name);
 var url = "ControllerName/ActionName/" + encodedName + "/" + keyword + "/" + description + "/" + linkUrl + "/" + includeMetrics + "/" + typeTask + "/" + project + "/" + userCreated + "/" + userModified + "/" + status + "/" + parent;; // + name + "/" + keyword + "/" + description + "/" + linkUrl + "/" + includeMetrics + "/" + typeTask + "/" + project + "/" + userCreated + "/" + userModified + "/" + status + "/" + parent;
Run Code Online (Sandbox Code Playgroud)

否则不要使用 encodeURIComponent 而是尝试在 ajax post 方法中传递参数

 var url = "ControllerName/ActionName/";   
 $.post(url,
        { name: nameVal, fkKeyword: keyword, description: descriptionVal, linkUrl: linkUrlVal, includeMetrics: includeMetricsVal, FKTypeTask: typeTask, FKProject: project, FKUserCreated: userCreated, FKUserModified: userModified, FKStatus: status, FKParent: parent },
 function (data) {.......});
Run Code Online (Sandbox Code Playgroud)

  • 这在当时可能是一个更好的解决方案:) (3认同)