简体   繁体   中英

working with '#!' in a ajax function url

ok I have created the below ajax request and I am having trouble passing the '#!' 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?

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 . Fragments do not get sent with XHR/ajax. According to the spec, XHR specifically excludes the url fragment from the response url . In your specific case you are using decodeURIComponent which is preserving the # sign, causing it to be stripped.

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 .


More info can be found on Wikipedia regarding url fragments.

MDN also has documentation on window.location.hash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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