Accessing C# DataObject data part from javascript - 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.

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?

Reading json file

In my ASP.NET backend I am creating json file from a query, I want to use this json file and take some data from it.
I wrote this code:
$(document).ready(function ()
{
$.getJSON("/Content/smartParkTotalJson.json", function (json)
{
});
}
And now I want to see the json , take some data from it (there are many landmarks inside this json file and I want to take the ID element, latitude and longitude).
How can I get access to the things I'm interested in?
In order to parse your json data, use JSON.parse(). Pretty-printing is implemented through JSON.stringify(); the third argument defines the pretty-print spacing.
$.getJSON("/Content/smartParkTotalJson.json", function(data)
{
var obj = JSON.parse(data);
var obj_str = JSON.stringify(obj, null, 2);
console.log(obj_str);
// ...
});
For reading/traversing json objects in javascript, take a look at this simple tutorial. It can represent a good starting point for understanding the basic principles, and unless you provide an example of your json data, also your only choice to perform your data extraction.

recieving broken JSON data in 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.

passing and retrieving variables in cakephp from javascript using GET METHOD

i know how to construct url parameters in javascript and i know how to extract it if im going to pass it to a php file.
However, here's the scenario, i am going to pass variables from javascript to a function. I have 2 textboxes and i can already get the values of these textboxes and store it to javascript variables:
var fname;
name = document.getElementById('fname');
var lname;
lname=document.getElementById('name');
now im stucked up on how i am going to construct it in a way that cakephp post method will be able to understand it. is it just the same with php like the code below?
var vars = "fname="+fname+"&lname="+lname;
here's my code:
if(window.XMLHttpRequest){
xmlVariable = new XMLHttpRequest();
}
else{
xmlVariable = new ActiveXObject("Microsoft","XMLHTTP");
}
xmlVariable.open("POST","save",true);
xmlVariable.send(vars);
"save" is actually a function in my cakephp where i wanna process the saving of "fname" and "lname" variables.
i need to do it in cakephp. Would there be any way?
Try with Params or data
Params variable is used to provide access to information about the current request.
public function save()
{
this->params['form']['lname'];
this->params['form']['fname'];
}
OR
Used to handle POST data sent from the FormHelper forms to the controller.
public function save()
{
this->data['lname'];
this->data['fname'];
}

JavaScript object - convert to JSON format and pass to a server side function

With the below code we can store our client side data in dictionary like object through JavaScript:
var Person = {};
Person["EmployeeID"] = "201";
Person["Name"] = "Keith";
Person["Department"] = "Sales";
Person["Salary"] = "25000";
Now I want to know how could I convert the Person object to JSON format and pass to our server side function and client side person object should serialize to server side person object.
My server side function looks like:
[WebMethod]
public static string Save(Person msg)
{
}
So please guide me how to convert the client side person object to JSON format as a result I can send the person data to our server side function with jQuery.
Well, the easy way would be to use jQuery's JSON plug-in and say $.toJSON(Person).

Categories

Resources