开发者

Missing Request parameters with UTF-8 when javascript escape() is used

I am using jquery ajax api to submit (POST/GET) a text as parameter to a processing page.

<input type="text" id="txtboxdata" name="txtboxdata" >

var dataString = "tmpVar="+ escape( $("#txtboxdata").val() );
$.ajax({ type: "POST", 
                  url: "processing.jsp",
                  data: dataString ,
                  dataType: "json",
                  success: function(){}
        });

//dataString holds the parameter and value to be passed. Value is retrieved from a text box.

If the text box

  • contains UTF-8 characters

    and

  • apply the javascript "escape" on the text box value

then the parameter tmpVar goes disappears from the request object in the processing page(processing.jsp).

I used the debugger, and checked the request object. The parameter called "tmpVar" never shows up.

This wor开发者_StackOverflow中文版ks correctly when the following conditions are met

tmpVar shows up in the request object of processing.jsp when

  • there are no UTF-8 characters.
  • I do not apply "escape" before making the ajax call.

My Question I would like to understand why does applying escape on a text containing UTF-8 not show up in the request object?

PS: I used "form serialize()" to solve the problem, just trying to understand why the issue occured.

Firebug: looks fine, this always shows up correctly. No missing data before the actual POST/GET.


You should call encodeURIComponent instead.


Using the answer provided by @SLaks did some more research and found a satisfactory answer.

Explains why "escape" shouldn't be used and compares it with other method which can be used.

  • encodeURIComponent()
  • encodeURI()

http://xkr.us/articles/javascript/encode-compare/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜