How do you pass an apostrophe through a URL?

后端 未结 4 762
时光取名叫无心
时光取名叫无心 2020-12-29 03:43

I\'m using Node.js:

var s = \'Who\\\'s that girl?\';
var url = \'http://graph.facebook.com/?text=\' + encodeURIComponent(s);

request(url, POST, ...)
         


        
相关标签:
4条回答
  • 2020-12-29 04:02

    Had the same problem, encodeURIComponent didn't encode single quote. The trick is to do the replacement of ' with %27, after the encoding:

    var trackArtistTitle = encodeURIComponent("Johnny Vegas - Who's Ready Fo'r Ice Cre'am")
    // result: Johnny%20Vegas%20-%20Who's%20Ready%20Fo'r%20Ice%20Cre'am
    trackArtistTitle = trackArtistTitle.replace(/'/g, '%27')
    // result: Johnny%20Vegas%20-%20Who%27s%20Ready%20Fo%27r%20Ice%20Cre%27am
    

    This way, trackArtistTitle will be properly decoded on server i.e. with PHP using urldecode().

    0 讨论(0)
  • 2020-12-29 04:08

    You can encode the single quote as specified in this link http://www.w3schools.com/TAGS/ref_urlencode.asp

    0 讨论(0)
  • 2020-12-29 04:12

    I know this doesn't address the OP's question, but for those coming here with OData Query related questions, note that the escape character is yet another single quote.

    unescapedValue.replace(/'/g, '\'\'')
    

    This assumes you have already performed an encodeURIComponent(unescapedValue) on your string

    Source: https://stackoverflow.com/a/4483742/2831961

    0 讨论(0)
  • 2020-12-29 04:21

    I'm doing a similar thing (also with Node.js) and first tried using JavaScript's built-in escape() function, but it didn't really work.

    Here's how I ended up getting search to work. It might just be a fluke:

     function doMySearch(showTitle) {
         showTitle = escapeShowTitle(showTitle)
         var url = "http://graph.facebook.com/search?q=" + showTitle + "&type=page"
         doSomethingWith(url)
    }
    
    function escapeShowTitle(title) {
        title = title.replace(/'/g, "")
        title = escape(title)
        return title
    }
    
    doMySearch("America's Funniest home Videos")
    
    0 讨论(0)
提交回复
热议问题