javascript - How to encode text using jquery? - Stack Overflow

I'm using ajax() to send POST request to a php page. This is the code:date = $.trim($('#d

I'm using ajax() to send POST request to a php page. This is the code:

  date     = $.trim($('#date').val()) 
  expiry   = $.trim($('#expiry').val()) 
  priority = $.trim($('#priority').val()) 
  note     = $.trim($('#note_text').val())

  $.ajax({
    type: "POST",
    url:  "client?method=addNote&id=10",
    data: "date="+date+"&expiry="+expiry+"&priority="+priority+"&note="+note,
    success: function(msg){
      alert(msg);
    }
  });

my problem is that the last variable named note could has many "strange" characters, like: & % $ / : ; , .

I have seen that the php page is no receiving all the "note" string correctly. If it found (&) it js "truncate" the string. how could I encode that text?

I'm using ajax() to send POST request to a php page. This is the code:

  date     = $.trim($('#date').val()) 
  expiry   = $.trim($('#expiry').val()) 
  priority = $.trim($('#priority').val()) 
  note     = $.trim($('#note_text').val())

  $.ajax({
    type: "POST",
    url:  "client?method=addNote&id=10",
    data: "date="+date+"&expiry="+expiry+"&priority="+priority+"&note="+note,
    success: function(msg){
      alert(msg);
    }
  });

my problem is that the last variable named note could has many "strange" characters, like: & % $ / : ; , .

I have seen that the php page is no receiving all the "note" string correctly. If it found (&) it js "truncate" the string. how could I encode that text?

Share asked Oct 19, 2011 at 10:25 DailDail 4,62816 gold badges77 silver badges113 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Don't pass a string, just pass the data.

data: { date: date, expiry: expiry, priority: priority, note: note },

If you were to pass a string, then you are building a URI by hand and that has nothing to do with jQuery, so you would use encodeURIComponent.

Try:

note = encodeURIComponent($.trim($('#note_text').val()));

See here for more on encodeURIComponent: https://developer.mozilla/en/JavaScript/Reference/Global_Objects/encodeURIComponent

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744917672a4600935.html

相关推荐

  • javascript - How to encode text using jquery? - Stack Overflow

    I'm using ajax() to send POST request to a php page. This is the code:date = $.trim($('#d

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信