JQuery - Ajax: encodeUriComponent not working (EncodeUri does)

10,765

Wouldn't it be a lot easier if there was some way to pass data when doing ajax calls, oh wait, there is, using the data option

$.ajax({
    type : "GET",
    url  : "Search.aspx",
    data : {
        action   : 'GetDocumentInfoByChronicleId',
        objectId : '09028139800c59e3',
        Db       : 'DIV_Firm'
    }
});

jQuery will create the querystring for you and escape it appropriately

As a sidenote, encodeURI is exactly what you should be using to encode a querystring containing ?, & etc. as encodeUriComponent will escape those characters as well, making the querystring invalid, but the method posted above is much simpler as you don't have to worry about encoding at all.

Share:
10,765
User999999
Author by

User999999

Updated on June 04, 2022

Comments

  • User999999
    User999999 almost 2 years

    In a small asp.net webclient I have the following Ajax-call.

    $.ajax({
        type: "GET",
        url: "Search.aspx?action=GetDocumentInfoByChronicleId&" + querystring
    })
    .success(function (msg) {
        $("#documentcontent").html(msg);
    })
    

    The querystring works for default characters but appears to be non-working when using special characters (see example below)

    objectId=09028139800c59e3&Db=DIV_Firm <== Works
    objectId=090281>>773c5983&Db=DIV_Firm <== Non Working
    

    Based on this (and many more posts on SO i opted to change my ajax-calls as follows (EncodeUriComponent). But none appear to be working (even with the original querystring).

    Could someone point out to me what i'm exactly doing wrong?

    $.ajax({
        type: "GET",
        url: "Search.aspx?action=GetDocumentInfoByChronicleId&" + encodeURIComponent(querystring)
    })
    .success(function (msg) {
        $("#documentcontent").html(msg);
    })
    

    Note: EncodeUri appears to be working though. But i'd prefer to use EncodeUriComponent