How to get JavaScript output as string in CefSharp? - javascript

I am trying to get the output of this javascript string but so far what I have tried did not work
My current code is this. response is blank what I want is 4
var task = chromiumWebBrowser1.EvaluateScriptAsync("2+2");
var response = task.Result;
MessageBox.Show(String.Format("output", response));
Any help would be great.

The EvaluateScriptAsync method returns Task<JavascriptResponse>. You can await the Task to obtain the JavascriptResponse object then get the Result or error.
JavascriptResponse response = await chromiumWebBrowser.EvaluateScriptAsync("1 + 1");
if (response.Success)
{
var onePlusOne = (int)response.Result;
}
else
{
var error = response.Message;
}

Related

forEach is not a function even with JSON.parse

I am having trouble with looping JSON data. I always get the error data.forEach is not a function even do i use JSON.parse. Anyone know how to fix this problem ?
function getApiGebruiker() {
CallWebAPI();
//nieuw deel
var url = "http://localhost:8081/persoons";
//do get request
var client = new HttpClient();
client.get("http://myIPadress/persoons", function (response) {
// do something with response
var data = JSON.parse(response);
console.log(data);
//data = JSON.parse(data);
let string = document.getElementById("pwd").value;
let loginn = document.getElementById("login").value;
data.forEach((persoons) => {
console.log(persoons.gebruikersnaam);
});
});
Your data seems to be an object ({}) and not an array ([]).
Try:
var data = JSON.parse(response);
var people = data._embedded.persoons
people.forEach((persoon) => {
console.log(persoon.gebruikersnaam);
});

How can i try to get json string when i receive something more that json string?

Good morning, folks.
I have a php using AJAX. For some updates been emplemented currently i'm using json, but sometimes we receive something like
'<div class="trigger error">Some error</div>{"1":{"message":"Success something."},"2":{"message":"There is update available"}}'
There is a way to write something like
try{
obj = JSON.parse(data);
function_to_print_result(obj);
} catch (e){
var string_json = REGEX(something(data));
var old_system_error = REGEX(something(data));
try{
obj = JSON.parse(string_json);
function_to_print_result(obj);
function_to_print_old_error(old_system_error);
} catch (e){
$('.msg').empty().html(data);
}
}
I'm trying understand REGEX but, realy, i have so much difficult. I need only REGEX.
Very thanks.
First of all, you really need to work on fixing the API response. There is no way valid JSON would accept HTML strings in it.
Now, try this temporary solution to extract the data and error message from the mixed value of the API response.
var data = '<div class="trigger error">Some error</div>{"1":{"message":"Success something."},"2":{"message":"There is update available"}}';
var obj;
try{
obj = JSON.parse(data);
function_to_print_result(obj);
} catch (e){
var old_system_error;
var regex = /^<[a-zA-Z"= ]+>[a-zA-Z ]+<\/+[a-zA-Z]+>(?={)/gi;
var string_json = data.replace(regex, function(match, token){
old_system_error = match;
return '';
})
try{
obj = JSON.parse(string_json);
console.log(obj);
console.log(old_system_error);
} catch (e){
console.log(e);
}
}

Remove characters from variable with Javascript

My variable looks as following:
response = "{ tradeofferid: '63341523' }"
I would like to remove all characters except for the letters and the semicolon.
I tried using the replace function but I get some errors.
function(err, response) {
if (err) {
throw err;
}
var result = response;
result = result.replace(/[{}]/g, "");
console.log(offerStatus);
res.end(result);
});
My console points at replace and the error log says: undefined is not a function
What I want to end up with is
response = "tradeofferid: 63341523"
response = { tradeofferid: '63341523' };
alert(response.tradeofferid);
for(var name in response) {
alert(name);
var value = response[name];
alert(value);
}
responseString = name + " : " + value;
alert(responseString);
You may try this but this answer is only specific to your question. This will not work if you have more than one attributes in "response" object.
response = "tradeofferid: " + JSON.parse(response)[tradeofferid]
... if you really want a string for display or something, but I'm guessing you actually just want to parse the JSON and pass the object around but haven't realized it yet.
You need to set something to be replaced in the .replace method. Try with:
var result = response.replace(/[^A-Za-z0-9\: ]/g, "")

Error While Updating parse.com Parse Object

We wrote the parse cloud function to update the object .
Until today morning it was working fine. Now our cloud code is not working . can you please check this ?
Below is our code to update the object which is throwing error
var point = new query(); for this line we are getting error like
TypeError: undefined is not a function
below is our full code
var query = Parse.Object.extend("Merchants");
var point = new query();
point.id = request.params.id;
point.set("keyfield1",request.params.keyfield1);
point.set("keyfield2",request.params.keyfield2);
point.set("keyfield3",request.params.keyfield3);
point.set("keyfield4",request.params.keyfield4);
point.set("keyfield5",request.params.keyfield5);
point.save(null,{
success:function(response)
{
var resp={};
resp.ResponseCode = "1000";
resp.data = response;
response.success(resp);
},
error:function(response)
{
response.error(response.status);
}
});
Can you please help us with this issue.
Try this:
var Merchants = Parse.Object.extend("Merchants");
var merchantQuery = new Parse.Query(Merchants);
merchantQuery.equalTo("<someKey>", <someVal>);
merchantQuery.find().then(...);
Hope it helps

Getting HTTP Response using restler in Node.js

I don't understand why this code doesn't work for me
var rest = require('restler');
var getResp = function(url){
var resp ="";
rest.get(url).on('complete',function(response){
resp = response.toString();
});
return resp;
};
I am not getting the response for getResp('google.com')
Am I missing something with restler?
Being that you're returning resp it looks like you've forgotten that the request is asynchronous. Instead try out this:
var rest = require('restler');
var getResp = function(url){
rest.get(url).on('complete', function(response){
console.log(response);
});
};
getResp('http://google.com/');
// output:
// <!doctype html><html itemscope="itemscope" ....
Because of its asynchronous nature it's preferred that you pass the value to a receiving callback. Take this small example:
var rest = require('restler');
// process an array of URLs
['http://google.com/',
'http://facebook.com/'].forEach(function(item) {
getResp(item);
});
function getResp(url){
rest.get(url).on('complete', function(response){
processResponse(response);
});
};
// data will be a Buffer
function processResponse(data) {
// converting Buffers to strings is expensive, so I prefer
// to do it explicitely when required
var str = data.toString();
}
Once the data comes in you'll be able to pass it around like any other variable.
return resp; is running before the on('complete' callback gets called because it is asynchronous. This is resulting is the resp variable never getting assigned a value.
Try this:
var rest = require('restler');
var getResp = function(url){
var result = rest.get(url).on('complete', function(response){
response;
});
return result;
};
getResp('http://google.com/');
You could also use a callback like in this SO answer.

Categories

Resources