Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing an ASP.NET web application using web services and Ajax.
In web service, I have a method where I get data in Arabic, Urdu from the database and convert it into JSON But at the client-side, I didn't get the complete JSON format data which through an error in jquery of "JSON Parse".

For English my code work correctly, it only throws an error whenever there is some Arabic, Urdu data.


IN database I have the table named as "dd_diaryDetail"

What I have tried:

C#
object dta = null;
using (DD_db db = new DD_db())
{
db.Database.Connection.Open();
 var filval = filtervalue.Split(':');
string cmd = "SELECT * FROM dd_diaryDetail WHERE (tcs_id = @tcsid) AND (dd_date = @dt)";
dta = db.Database.SqlQuery<diarydetailview>(cmd, new SqlParameter("@tcsid", filval[0]),new SqlParameter("@dt", filval[1])).ToList();
if (dta != null)
{
db.Database.Connection.Close();
var res_data = js.Serialize(dta);
Context.Response.Clear();
Context.Response.ContentType = "application/json; charset=utf-8";
Context.Response.AddHeader("content-length", res_data.Length.ToString());
Context.Response.Write(res_data);
Context.Response.Flush();
}
db.Database.Connection.Close();
}

The result I get an incomplete JSON array
[{"dd_id":8,"tcs_id":8,"dd_date":"02-03-2020","CW_unitno":"44","CW_title":"ajdsaj","CW_pageno":"۳۳","CW_qno":"۸۸","CW_activity":"طگطگشدش","HW_unitno":"۷۷","HW_title":"چفچفچف","HW_pageno":"۷۷","HW_qno":"۹۹","HW_activity":"شفچففچففچفچ","Tst_unitno":"۵۵۵","Tst_title":"چفچفشش","Tst_pageno":"۴۴","Tst_qno":"۳۳","Tst_activity":"رچرچ"
Posted
Updated 3-Mar-20 2:50am
v2
Comments
[no name] 2-Mar-20 15:19pm    
What kind of SQL Server are you using? Very strange to hit something like this these days...

Quote:
C#
Context.Response.AddHeader("content-length", res_data.Length.ToString());
This line is the problem.

The content length header specifies the number of bytes in the response body. You are sending the number of characters instead.

That mostly works when you send English text, because that only tends to use characters which can be represented as single bytes. But when you're returning characters which require multiple bytes to encode them, the string length will be less than the number of bytes.

UTF-8 - Wikipedia[^]

Most browsers will truncate the response body when it reaches the number of bytes specified in the content length header, which is why you're getting an incomplete response.

The simplest option would be to remove the line which sets the content length header. Otherwise, you're going to need to know exactly how many bytes you're sending:
C#
string res_data = js.Serialize(dta);
byte[] res_bytes = System.Text.Encoding.UTF8.GetBytes(res_data);

Context.Response.Clear();
Context.Response.ContentType = "application/json; charset=utf-8";
Context.Response.AddHeader("content-length", res_bytes.Length.ToString());
Context.Response.BinaryWrite(res_bytes);
Context.Response.Flush();
 
Share this answer
 
There are multiple levels that you could be having the breakdown on this one; and it is going to take troubleshooting on your part to determine where the problem is.

1. After your EF query, what are the contents of dta? How many records are there versus how many should there be?

2. Inspect an individual instance of diarydetailview within dta. Is it complete? Repeat over a few and the instance which is throwing the issue.

3. Check and validate the content of res_data as well as the length.

Other things....

Did you know that you are closing the db.Connection twice if dta is populated?

What version of NET are you using? Apparently not all JSON serializers are the same:
Reference: c# - Serializing a list to JSON - Stack Overflow[^]

What browser are you using? And how are you calling your AJAX (jquery/ version)? This comes back to the data/length question above:
Reference: ajax - WCF service returns incorrect Content-Length when using gzip encoding - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900