[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2664
  • Last Modified:

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 ' " { } []
0
CahitEy
Asked:
CahitEy
  • 5
  • 5
  • 4
  • +2
2 Solutions
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now