URL编码从JS到Drupal的字符串

Cri*_*ris 4 javascript uri drupal url-encoding

我有一个使用Ajax的Drupal Web应用程序; 有时,Ajax函数需要将字符串作为参数传递给Drupal函数

$.ajax({url: "index.php?q=get_value/"+encodeURIComponent(value),  
Run Code Online (Sandbox Code Playgroud)

当值包含斜杠时,无法从Drupal函数中识别出

 function get_value($value) {
       print urldecode($value);
Run Code Online (Sandbox Code Playgroud)

例如,如果传递的字符串是ABC/123,则get_value仅打印ABC

如何解决这个问题传递斜杠并从PHP/Drupal获取整个字符串?

Vis*_*hal 6

使用%2f而不是/:

$.ajax({url: "index.php?q=get_value" + "%2f" + encodeURIComponent(value),  
Run Code Online (Sandbox Code Playgroud)

[ 参考 ],见2.2节

或者,更可读的替代方案:

$.ajax({url: "index.php?q=" + encodeURIComponent("get_value/"+value),
Run Code Online (Sandbox Code Playgroud)

  • 一个更具可读性的等价物:$ .ajax({url:"index.php?q ="+ encodeURIComponent("get_value /"+ value), (3认同)