admin管理员组

文章数量:1405516

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

本文标签: javascriptHow to encode text using jqueryStack Overflow