pass special chars to webservice with json

how could i send a string like this :

hello "world" i'm good

with jquery :
 data:  "{'txt':'" + txt + "'}",

this chars encounter error ' " { } []
LVL 1
CahitEyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Albert Van HalenAnalyst developerCommented:
Use this
data: { "txt": txt },

Open in new window

0
CahitEyAuthor Commented:
no not worked
0
Albert Van HalenAnalyst developerCommented:
What is the error you're getting?
I did create a test script myself and is working...
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

CahitEyAuthor Commented:
The code as givn above
var txt = $("#inp").val();
var id= $("#id").val();
 data:  "{'id':'" + id + "','ad':'" + txt + "'}",

the error occurs only when i use special chars
 {'id':'14','ad':'Türkiye'de Kadin Cinsel Islev Bozukluklari'}



{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (26): {\u0027id\u0027:\u002714\u0027,\u0027ad\u0027:\u0027Türkiye\u0027de Kadin Cinsel Islev Bozukluklari\u0027}","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

Open in new window

0
Albert Van HalenAnalyst developerCommented:
Ok so your issue is server side.
.NET gives the error while deserializing the string...

Need some more time to investigate.
Perhaps it's wise to add .NET as a zone to your question...
0
CahitEyAuthor Commented:
no it does not goes to .net becaues of ' -> char i made line by line debugging and  this char split the json source
0
Albert Van HalenAnalyst developerCommented:
Please be clear...
Is the error you're getting serverside or clientside.
Also, you're still creating the data object as a string. Why not according to my previous comment?

Splitting the JSON source? where ?
Please provide full source
0
complexcarbCommented:
Is this part of an AJAX call? If so can you post the entire call? Or provide a link to the page so we can look for ourselves?
0
hieloCommented:
try:
var txt = $("#inp").val().replace(/[']/g,"\\"+"'");
var id= $("#id").val().replace(/[']/g,"\\"+"'");


 data:  "{'id':'" + id + "','ad':'" + txt + "'}",

Open in new window

0
leakim971PluritechnicianCommented:
Hello CahitEy,

Try :

data:  "{\"id\":\"" + id + "\",\"ad\":\"" + txt + "\"}",

Regards.
0
hieloCommented:
>> data:  "{\"id\":\"" + id + "\",\"ad\":\"" + txt + "\"}",
That will still give a syntax error when txt has a double quote in it. Whatever enclosing character is used for the values, that character needs to be encoded within the value, which is what I did above with the replace.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leakim971PluritechnicianCommented:
>That will still give a syntax error when txt has a double quote in it.
Hello @hielo,

Check page 14-15 here : http://www.json.org/json.pdf
Any UNICODE char except \ and "


A feedback from CahitEy, assuming the current json string seems to fail not because there's bad char inside but because the json string is malformed (the simple quote) :
The parser reject it : {"Message":"Invalid object passed in, \u0027[b]:[/b]\u0027 or \u0027[b]}[/b]\u0027 [b]expected[/b]



0
hieloCommented:
>>Hello @hielo
Hello to you too.

>>Check page 14-15 here : http://www.json.org/json.pdf
Did

>>Any UNICODE char except \ and "
If the variable txt can be GUARANTEED to not have double quotes (or any special char for that matter), then the rule that applies (on page 15 of that document) is the one you pointed out:
Any UNICODE char except \ and "

But if it does contain \ or " ( or any of the special chars - after all, json wouldn't be very helpful if you were not allowed to pass along any of those special chars), then you need to apply the encoding rules below it. So in case of \ and ", you need:
\-> \ (when there is a \ as part of the value)
\ -> " (when tehre is a " as part of the value)

where the "->" means "followed by".  

You are focusing only on the first "rule", but is not correct. You need to look/consider all the rules when encoding the string.
0
leakim971PluritechnicianCommented:
I've not saw ID:28454032 before now... You right we need to [b]escape[/b] all specials chars. Thanks, I will give you the points :))
0
CahitEyAuthor Commented:
Sorry for lateness i am checking immediately
0
CahitEyAuthor Commented:
Very very very thank you
0
leakim971PluritechnicianCommented:
Thanks for the points!
0
hieloCommented:
>>Thanks for the points!
waiting :)
0
leakim971PluritechnicianCommented:
xlol
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.