3

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

Community
  • 1
  • 1
User999999
  • 2,500
  • 7
  • 37
  • 63
  • Where are you getting the querystrings from? There's a built in way to pass data in $.ajax, no need for querystrings. – adeneo May 09 '14 at 07:40
  • Querystring are being built by myself. I need to send data to server based on the userinput. What do you mean with "There's a built in way to pass data in $.ajax?" Do you mean using ´{data: ... }? – User999999 May 09 '14 at 07:41

1 Answers1

8

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.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Will try so directly! I'll get back to you soon. – User999999 May 09 '14 at 07:44
  • 1
    Sweet! it works! Thanks Man! I didn't know ´$.ajax - data escapes´ it automatically :) i'll approve the answer as soon as SO allows me too – User999999 May 09 '14 at 07:49
  • It does, arrays, objects, strings and whatever can be passed directly to $.ajax, and jQuery will try to make sense of it and create a valid querystring. – adeneo May 09 '14 at 07:51
  • I don't know if at one point jQuery didn't automatically do this, but there are many S/O questions about this with answers directing users TO encode values AND pass them via `data: { .. }` – JoeBrockhaus Feb 23 '15 at 19:51
  • @JoeBrockhaus - jQuery has done this for many years, if you pass it a value it will convert it into `form/www-endoded` data, but some people have controllers that expect JSON on the backend, and then you'd have to set a content header and stringify the object before sending it. As long as the server expects a form, this is the way to go. – adeneo Feb 23 '15 at 20:31