I'm calling a webservice using jQuery with .ajax
Here are the data parameters for the call:
var parameters = "{'Titre':'" + Titre + "','Description':'" + Description + "','Contact':'" + Contact + "','VilleId':'" + VilleId + "','QuartierId':'" + QuartierId + "','UserId':'" + UserId + "'}";
It works fine. But when parameters Description
or Titre
contain the '
character , no call!!!
Does anyone have an idea how can i make it work even with apostrophe character in Titre
and/or Description
?
I'm calling a webservice using jQuery with .ajax
Here are the data parameters for the call:
var parameters = "{'Titre':'" + Titre + "','Description':'" + Description + "','Contact':'" + Contact + "','VilleId':'" + VilleId + "','QuartierId':'" + QuartierId + "','UserId':'" + UserId + "'}";
It works fine. But when parameters Description
or Titre
contain the '
character , no call!!!
Does anyone have an idea how can i make it work even with apostrophe character in Titre
and/or Description
?
- Possible duplicate of stackoverflow./questions/1470768/… – Lance Commented Jan 28, 2011 at 17:29
5 Answers
Reset to default 3I would use a json encoder. Douglas Crockford's JSON in JavaScript seems a good choice.
Then you just write
var param = JSON.stringify({ 'Titre': Titre, 'Description': Description });
and let the master worry about the quoting.
Try escaping the apostrophe:
var parameters = "{
'Titre':'" + Titre.replace(/'/g,"\'") +
// ^
"','Description':'" + Description +
"','Contact':'" + Contact +
"','VilleId':'" + VilleId +
"','QuartierId':'" + QuartierId +
"','UserId':'" + UserId + "'}";
You probably need to encode the values to be safely passed in a URL.
http://plugins.jquery./project/URLEncode
You can try escaping it:
var str = "asdfsd'asdfadf";
str = str.replace("'", "\'");
Here's the way I escape that works for me currently:
var theString = "O'Kief blahblahblahblah";
theString = theString .replace("'", "\\'");
//Note the double \\
Doesn't break and saves as: O'Kief blahblahblahblah
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744213354a4563439.html
评论列表(0条)