im trying to return a string value from a method inside my script tag however it always returns an object and i cant get at the string value.
Here is the code:
i retrieve the object returned from a webservice call;;
private function getNameResults(e:ResultEvent):String{
var name:Array = new Array(e.result);
var site:String = site_names[0].toString();
Alert.show("site - " +site);
return site;
}
the alert prints out the name fine, however when i try use the value in my next method (which calls the web service which calls getNameResults) i get the object tag
private function getInfo(roomName:String):String{
var site:String =userRequest.getRoomZoneInfo(roomName);
return site;
}
however the value returned here is [object AsyncToken]
any ideas?
You are setting the result of getRoomZoneInfo() to the variable site, but you are casting it as a String. getRoomZoneInfo is returning an object, and because the variable you are sticking it in is a string it is like calling .toString() on the object, which yields the [object AsyncToken].
Basically if getRoomZoneInfo is a webservice call, you cannot get the information you are looking for here, you have to wait until the result comes back and get the string you are looking for there. Make sense?
Your getInfo() method isn't calling getNameResults(). It's calling getRoomZoneInfo(). I don't know what that method does, but I'm guessing it's returning an Object, not a String.
I m getting [ObjectAsyncToken] in variable a instead of string, as I'm retrieving data from database
private function init():void
{
ro.initView();
var a:String=String(ro.getID());
}
database function:
public void initView()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:alarmdsn"," "," ");
Statement s = conn.createStatement();
ResultSet r = s.executeQuery("select * from alarm");
while(r.next()) {
a = r.getString(1);
}
}
catch(Exception e) {}
}
public String getID() {
return a;
}
Related
The result I am getting
falsef005fdc9-7781-4c75-9f54-0a05a7fc6e71bc3d0990-2f44-491f-a25a-3771772d6a61false2017-10-10T08:43:16.0831010019false
I am getting like the format as below when I use the postman.
Question: How do I remove []? I tried to remove ToList() but it didn't work.
Question: Why I am getting a string like result instead of json?
controller method
public IEnumerable<ClientModel> GetClients()
{
var clients = ClientHelper.GetAllClients();
var result = (from c in clients
select c).ToList();
return Mapper.Map<IEnumerable<ClientModel>>(result);
}
Use this namespace
using System.Web.Script.Serialization;
and
var json_result = new JavaScriptSerializer().Serialize(result );
You must get json data in json_result
I'm trying to receive an answer from a WCF method from the client. When I try to execute void methods, they are working fine. For example:
Uri u = new Uri(string.Format(LogIn.ctx.BaseUri + "/CreateRole?name='{0}'",
TextBox1.Text), UriKind.RelativeOrAbsolute);
LogIn.ctx.Execute(u, "GET");
Now I want to call a method which returns a boolean, and this value will be used. Here's the method I want to call and receive its returned value:
[WebGet]
public bool Controler(string role, string user)
{
if (Roles.IsUserInRole(user, role))
{
return true;
}
else
{
return false;
}
}
Instead of LogIn.ctx.Execute(u, "GET"), try this:
IEnumerable<bool> result = LogIn.ctx.Execute<bool>(u);
bool answer = result.Single();
I'm using pageList (the one made by troy goode), on his example he has
// in this case we return IEnumerable<string>, but in most
// - DB situations you'll want to return IQueryable<string>
private IEnumerable<string> GetStuffFromDatabase()
{
var sampleData = new StreamReader(Server.MapPath("~/App_Data/Names.txt")).ReadToEnd();
return sampleData.Split('\n');
}
Since i'm using a database i changed to IQueryable, but, i don't know what to write inside to return the data from the database, i tried changing the path to ~/App_Data/DatabaseName.sdf but i get that sampleData.Split('\n');
cannot implicitly convert type string to system.linq.iqueryable<string>
How i can change that?
return sampleData.Split('\n').AsQueryable();
The return type of your method is IEnumerable and requires to return as Enumeration
so please do this way
return sampleData.Split('\n').AsEnumerable();
I need to make a function that execute a java method and return his result. It is static becouse a lot of other functions will call this function. So I did this:
public static function FKDescription(dest:String):String{
var jRemote:RemoteObject = new RemoteObject();
var s:String;
jRemote.destination = dest;
jRemote.getValues.addEventListener(ResultEvent.RESULT,valresult);
jRemote.getValues();
function valresult(event:ResultEvent):void{
s = event.result as String;
}
return s;
}
But the function returns null, because the valresult() was not been called at the end of main function. What shoud I do to make FKDescription() return the string that came from remoteobject?
Tanks.
That's because HTTP calls are asynchronous, so you have to way for the result. What you want to do is remove the result handler to it's own function so that it waits for the result, then does something with it. It is NOT possible to do what you're trying to accomplish right now, which is returning the value right away.
Check here on how to do async calls.
as J_A_X said, all the http requests are async, i suggest to refactor your code this way:
public static function FKDescription(dest:String, callback:Function):String{
var jRemote:RemoteObject = new RemoteObject();
var s:String;
jRemote.destination = dest;
jRemote.getValues.addEventListener(ResultEvent.RESULT,valresult);
jRemote.getValues();
function valresult(event:ResultEvent):void{
callback(event.result as String);
}
}
and in the caller, instead of:
ret = FKDescription("something");
otherFunction(ret);
you can do this:
FKDescription("something", otherFunction);
I have an asp.net page with a WebMethod on it to pass JSON back to my javascript.
Bellow is the web method:
[WebMethod]
public static string getData(Dictionary<string, string> d) {
string response = "{ \"firstname\": \"John\", \"lastname\": \"Smith\" }";
return response;
}
When this is returned to the client it is formatted as follows:
{ \"d\": \"{ \"firstname\": \"John\", \"lastname\": \"Smith\" }\" }
The problem is the double quotes wrapping everything under 'd'. Is there something I've missed in the web method or some other means of returning the data without the quotes? I don't really want to be stripping it out on the client everytime. Also I've seen other articles where this doesn't happen.
Any help would be appreciated thanks.
I assume that you want to return the JSON representation of the object
{
firstname:"John",
lastname:"Smith"
}
but your method signature is returning a string. The ASP.Net framework serialisation is correctly serialising the string response. Put another way, if your function was
string response = "foo";
return response;
You would not be surprised if the output was
{"d":{"foo"}}
It just happens that response has double quotes that need to be escaped.
You obviously just want to get at the object. You have 2 options: -
1) use eval in your javascript to turn the string into an object e.g.
function onSuccessCallback(retval) {
var obj = eval(retval.d);
}`
2) or (and this is my prefered solution) have your method return an actual object and let the JSON serialisationof the framework do the heavy lifting for you
[WebMethod]
public static object getData(Dictionary<string, string> d) {
var response = new { firstname = "John", lastname="Smith" };
return response;
}
You will see that this generates the response that you probably originally expected (e.g.
{"d":{"firstname":"John", "lastname":"Smith"}}
Actually this entire issue exists because you're trying to out-think ASP.Net web services. You need to setup a class for your data to be returned and use that class (or List(of YourClass)) to queue up results and return them.
A great article explaining all this (a very common pitfall) is: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/
I had a similar issue with my code. I was trying to return an XmlDocument as JSON to a calling script but returning XmlDocument from the WebService returned an empty set of arrays (as XmlDocument is NOT serializable!).
If i set the ScriptService with the attribute ResponseFormat.JSON then my JSON was double-escaped.
The way to out-fox ASP.NET is to tell ASP.NET that you're returning XML and then it won't double-escape your JSON ;-)
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public String MyJSONWebService(String sParam1, String sParam2, String sParam3)
{
... do stuff.....
XmlDocument oXMLDocument = new XmlDocument();
oXMLDocument.LoadXml(sXML);
sJSON = JsonConvert.SerializeXmlNode(oXMLDocument.SelectSingleNode("autnresponse"));
return sJSON;
}
I know it's a hack but .....