recieving broken JSON data in javascript - javascript

I have PageMethod in javascript which is receiving JSON data from C#.
In C# its getting full xml data from database and converting into JSON and sending back to PageMethod.
JSON Converted data is about 33kb, but i'm not able to receive full data in javascript. I'm receiving only 9 kb of data. any solution for getting full data in java script.
PageMethod.methodName(onSuccess,OnFail);
function OnSuccess(result)
{
alert(result);
}
function OnFail()
{
alert("Error");
}
C# code as follows,
ParamResult objParamResult = new ParamResult();
objParamResult.ResultDt = string.Empty;
DataTable XmlMainSub = objCBTag.getParamPickupDetailsDB();
string myData = XmlMainSub.Rows[0][0].ToString();
XmlDocument doc = new XmlDocument();
doc.LoadXml(myData);
string jsonText = JsonConvert.SerializeXmlNode(doc);
return jsonText;

instead of
string jsonText = JsonConvert.SerializeXmlNode(doc);
you can use
string jsonText = new JavaScriptSerializer().Serialize(doc).toString();
you need to use namespace for this
using System.Web.Script.Serialization;

After i made lot of research, i found that its not possible to send JSON data from C# to javascript which is more than 8KB or 9KB in size.
And i solved this problem by making use of c# generics which is Dictionary which contains Key and Value Pair. I Tried to loop XML Data which is coming from database and stored in a dictionary object.
Then i passed it to javascript. There i able to receive full data without any error.

Related

GetMethod.getResponseBodyAsString() to xml object

using Javascript HttpClient i am running a get method on WebService which works fine and then i store Response in a variable resp_va as shown below.
snip from code below.
var httpGetMethod = new GetMethod(url);
httpClient.executeMethod(httpGetMethod);
var statuscode = httpGetMethod.getStatusCode();
var resp_va = httpGetMethod.getResponseBodyAsString();
although output is in XML Format but to me Looks like it is string and therefore i am unable to parse it. Question is how can i convert "resp_va" in XML object before parsing it further?

How to parse XML formatted object in PHP and build a JSON object to be sent to the client?

I am writing a php script to parse the XML formatted object and extract the necessary fields. I want to build a JSON object to be sent to the client. To do that I am putting the extracted data from XML object to a php array, which I will be encoding and sending to javascript. But when I try to echo the data from the php array, my PHP script crashes. I am getting the following error : ERR_EMPTY_RESPONSE
Where am I going wrong? Is there a better approach to get parse XML-formatted object in php and extract necessary fields and build a JSON object to be sent to client?
The code is as follows:
$note = "https://seekingalpha.com/api/sa/combined/".$symbol.".xml";
$xml=simplexml_load_file($note) or die("Error: Cannot create object");
$newsHeadline = $xml->channel->item[1]->title->__toString();
$newsLink = $xml->channel->item[1]->link->__toString();
$newsInfo = array("Title"=>$newsHeadline,"Link"=>$newsLink);
$jsonNews = json_encode($newsInfo);
echo $newsInfo('Title');

Accessing C# DataObject data part from javascript

Is it possible to access the C# DataObject data from javascript?
DataObject data = new DataObject();
data.SetText(stringBuilder.ToString());
data.SetData("application/json", itemsJson);
Clipboard.Clear();
Clipboard.SetDataObject(data);
var x = Clipboard.GetData("application/json");
In the C# side, x will be the json string. But it lost at js side.
EDIT
With
text = ev.clipboardData.getData("text/plain");
I've got the text, but:
var json = ev.clipboardData.getData("application/json");
nothing will be in json.
UPDATE
As I see, I can not access the data part of the dataobject from javascript. I think this is for some security reason, but not sure.

How to create ajersey restful method that can accept three arrays of strings

I have been trying to send string arrays to a restful services without any luck. I have written this
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getBackgroundImages(#QueryParam("missions") String[] missions,
#QueryParam("objects")String[] objects,
#QueryParam("dates")String[] dates) {
........
return generateTxt();
}
on the javascript side I have this
var missions = new Array("One", "Two");
var objects = new Array("objOne" ,"objTwo");
var dates = new Array("1967-11-07","1977-12-17");
$.ajax({
url: "myurl/rest/UploadBackgroundFile/",
data: {'missions':missions,'objects':objects,'dates':dates},
success: function (data) {
arr = JSON.parse(data);
$('.container-fluid').css('background-image','url('+arr[0].img+')');
}
});
my problem is that this is not working and I am getting this exception
org.apache.catalina.core.ApplicationContext.log StandardWrapper.Throwable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type
public java.lang.String UploadBackgroundFile.getBackgroundImages(java.lang.String[],java.lang.String[],java.lang.String[])
if I change the parameters to normal string and send strings from the javascript side than the method will work.
so the question is how to send and receive string arrays from the jquery ajax to the jersey restful method.
with regards,
es
Server side, you have to change the string array to a List<String> to make it work.
Client side, you can see this to help you how to send the datas. I know in title it's write PHP, but it's well explained.

Retrieving values of json object in javascript

I am sending an AJAX request to server and retrieving a response as a json object from the server with javascript code to my android application. I know the key values of the json object(ID, name, status, etc.) but I do not know how to get their values.(100, Mark, success, etc.) I need those data for processing for some other task. Can someone please tell me how to extract data from that json object. When I use alert(http.responseText); as follows I get the json object displayed. I need to get the values out of it.
method used to receive response
http.onreadystatechange = function() { //Handler function for call back on state change.
if(http.readyState == 4) {
alert(http.responseText);
json object I receive
{"header": {"ID":100,"name:"Mark"},"body":{"status":"success"}}
You have to convert The string to an object by doing var response=JSON.parse(http.responseText);
Just treat it like any object - console.log(response['name'])
You need to convert it to a JavaScript object using JSON.parse:
var obj = JSON.parse(http.responseText);
Since some legacy browsers do not have native JSON support you should include json2.js to shim it for those browsers.
you will have to eval the http.responseText to get the json object...
but using eval is not recommended, so you can use the json2 library to parse the text into json object..
or else you can even use the library like jquery and use function parseJSON to get it converted to json object

Categories

Resources