简体   繁体   English

使用“#!” 在ajax函数网址中

[英]working with '#!' in a ajax function url

ok I have created the below ajax request and I am having trouble passing the '#!' 好的,我创建了以下ajax请求,但无法传递“#!” characters to the browser from the response created by the function. 函数创建的响应向浏览器输入字符。 When I view the console in the web browser the '#!' 当我在网络浏览器中查看控制台时,“#!” characters are completely removed from the url. 字符已从网址中完全删除。 Is it possible to keep these characters in the url? 是否可以将这些字符保留在url中?

Code: 码:

$(function() {
function loadStats() {

  $.ajax({
    url: 'http://stats.nba.com/players/gamelogs/'+decodeURIComponent('#!')+'?&callback=?',
    jsonpCallback: 'jsonReturnData',
    dataType: 'jsonp',
    data: {
      CF: 'PLAYER_NAME*E*James Harden',
      Season: '2016-17',
      SeasonType: 'Regular Season',
      format: 'json'
    },

    success: function(response) {
      console.log(response);

    } //success

  }); //AJAX Call
} //load Chart

loadStats();

});

This is because url fragments (denoted by everything following the # sign) are client-side only . 这是因为url片段(由#符号后面的所有内容表示) 客户端 Fragments do not get sent with XHR/ajax. 片段不会与XHR / ajax一起发送。 According to the spec, XHR specifically excludes the url fragment from the response url . 根据规范,XHR专门从响应url中排除url片段 In your specific case you are using decodeURIComponent which is preserving the # sign, causing it to be stripped. 在您的特定情况下,您使用的是decodeURIComponent ,它保留#号,从而导致其被剥离。

Fragments are only evaluated client-side after the requested resource is returned from the server. 从服务器返回请求的资源后,仅在客户端评估片段。

You may be able to circumvent this by using encodeURIComponent . 您可能可以通过使用encodeURIComponent规避此问题。


More info can be found on Wikipedia regarding url fragments. 可以在Wikipedia上找到有关URL片段的更多信息。

MDN also has documentation on window.location.hash MDN在window.location.hash上也有文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM