undefined object in retrieving data aspnetcore - asp.net-core

I'm having trouble in binding the records from json to my
script in my view but I'm getting data from console.log. Can you
please help me what am I missing? here's my code...
public JsonResult GetListOfQuestions()
{
List<Question> QuesList = _context.Question.ToList();
return Json(QuesList);
}
<div id="SetQuestionsList">
<div id="LoadingStatus">
</div>
</div>
<script>
$("#LoadingStatus").html("Loading....");
$.get("/Questions/GetListOfQuestions", null, DataBind);
function DataBind(QuesList) {
var SetData = $("#SetQuestionsList");
console.log(QuesList);
for (var i = 0; i < QuesList.length; i++) {
var aa = i + 1
var Data =
"<div class='row_{" + QuesList.QuestionId + "}'>" +
"<div class='panel panel-primary'>" +
"<div class='panel-heading'>" +
"<div class='row'>" +
"<div class='col-lg-10'>" +
"<p>Question " + aa + "</p>" +
"</div>" +
"<div class='col-lg-1'>" +
"<a href='#' class='btn btn-warning' onclick='EditQuestion({" + QuesList.QuestionId + "})'>" +
"<span class='glyphicon glyphicon-edit'></span>Update" +
"</a>" +
"</div>" +
"<div class='col-lg-1'>" +
"<a href='#' class='btn btn-danger' onclick='DeleteQuestionRecord({" + QuesList.QuestionId + "})'>" +
"<span class='glyphicon glyphicon-trash'></span>Delete" +
"</a>" +
"</div>" +
"</div>" +
"</div>" +
"<div class='panel-body'>" +
"<div class='row'>" +
"<div class='col-lg-12'>" +
"<div class='form-group'>" +
"<b>Description</b><br />" +
"<p>" + QuesList.QuestionText + "</p>" +
"</div>" +
"</div>" +
"</div>" +
"</div>" +
"</div>" +
"</div>";
SetData.append(Data);
$("#LoadingStatus").html(" ");
}
}
</script>

Assuming you are using a version of jQuery >= 1.5, the following should help you determine what's going on. The way the script is currently written, using the ajax shorthand version, if there is an error in the call, jquery will fail silently. This way, you have an option to handle an error during the call execution and view the error in the browser dev console to help you debug the issue.
Update I added a jsfiddle for you to understand it better in case the explanation isn't enough (you should provide a jsfiddle in the future)
Also, see the following json, I'm assuming your data is similar to https://api.myjson.com/bins/1fryq4. You have to take into account the data structure that is being converted into json from the backend.
function getQuestions(){
$("#LoadingStatus").show();
$.get("https://api.myjson.com/bins/1fryq4")
.done(function( data ) {
//call you binding function
DataBind(data);
})
.fail( function(err){
//handle the error in the UI, alert the user something went wrong, recover form the error
alert("GetListOfQuestions error!");
//chrome: display object in js console
console.dir(err);
})
.always(function() {
//hide loading spinner
$("#LoadingStatus").hide();
});
}
Databind js function (shortened, see jsfiddle)
function DataBind(QuesList) {
var aa = 0;
var QuestionList = $("#SetQuestionsList");
//if you need to convert json to a js object use the builtin parser
//Example: var data = JSON.parse(QuesList);
//TODO good practice to check if valid list before trying to iterate the array
//pay attention to your incoming object
QuesList.questions.forEach(function(item){
aa++;
var question =
"<div class='row_{" + item.questionId + "}'>" +
"<div class='panel panel-primary'>" +
"<div class='panel-heading'>" +
"<div class='row'>" +
"<div class='col-lg-10'>" +
"<p>Question " + aa + "</p>" +
"</div>" +
"<div class='col-lg-1'>" +
"<a href='#' class='btn btn-warning' onclick='EditQuestion({" + item.questionId + "})'>" +
"<span class='glyphicon glyphicon-edit'></span>Update" +
"</a>" +
"</div>" +
"<div class='col-lg-1'>" +
"<a href='#' class='btn btn-danger' onclick='DeleteQuestionRecord({" + item.questionId + "})'>" +
"<span class='glyphicon glyphicon-trash'></span>Delete" +
"</a>" +
"</div>" +
"</div>" +
"</div>" +
...
"<b>Description</b><br />" +
"<p>" + item.questionDescription + "</p>" +
"</div>" +
...
"</div>";
QuestionList.append(question);
});
}

<script type="text/javascript">
function getQuestions() {
$("#LoadingStatus").show();
$.get("/Questions/GetListOfQuestions")
.done(function (data) {
//call you binding function
console.log(data)
DataBind(data);
})
.fail(function (err) {
//handle the error in the UI, alert the user something went wrong, recover form the error
alert("GetListOfQuestions error!");
//chrome: display object in js console
console.dir(err);
})
.always(function () {
//hide loading spinner
$("#LoadingStatus").hide();
});
}
function DataBind(QuesList) {
var aa = 0;
var QuestionList = $("#SetQuestionsList");
console.log(QuesList);
//if you need to convert json to a js object use the builtin parser
//Example: var data = JSON.parse(QuesList);
//TODO good practice to check if valid list before trying to iterate the array
//pay attention to your incoming object
QuesList.questions.forEach(function (item) {
aa++;
var question =
"<div class='row_{" + item.QuestionId + "}'>" +
"<div class='panel panel-primary'>" +
"<div class='panel-heading'>" +
"<div class='row'>" +
"<div class='col-lg-10'>" +
"<p>Question " + aa + "</p>" +
"</div>" +
"<div class='col-lg-1'>" +
"<a href='#' class='btn btn-warning' onclick='EditQuestion({" + item.QuestionId + "})'>" +
"<span class='glyphicon glyphicon-edit'></span>Update" +
"</a>" +
"</div>" +
"<div class='col-lg-1'>" +
"<a href='#' class='btn btn-danger' onclick='DeleteQuestionRecord({" + item.QuestionId + "})'>" +
"<span class='glyphicon glyphicon-trash'></span>Delete" +
"</a>" +
"</div>" +
"</div>" +
"</div>" +
"<div class='panel-body'>" +
"<div class='row'>" +
"<div class='col-md-6'>" +
"<div class='form-group'>" +
"<b>Question Type</b><br />" +
"<text>" + item.QuestionTypeName + "</text>" +
"</div>" +
"</div>" +
"</div>" +
"<div class='row'>" +
"<div class='col-lg-12'>" +
"<div class='form-group'>" +
"<b>Description</b><br />" +
"<p>" + item.QuestionDescription + "</p>" +
"</div>" +
"</div>" +
"</div>" +
"</div>" +
"</div>" +
"</div>";
QuestionList.append(question);
});
}
</script>

Related

Getting variable from webview to react native

I have a function in a webview which has response:
var respMsg = function(e)
{
var respData = eval("(" + e.data + ")");
document.getElementById("response").innerHTML = e.origin + " SENT " + " - " +
respData.responseCode + "-" + respData.dataKey + "-" + respData.errorMessage;
document.getElementById("response").style.display = 'none';
}
My question is: how to get for example respData.dataKey in a react native value so I can use it further (for example to get it and log its value)

JQuery AJAX call gives success, but response undefined

I have a rudimentary page from which I am calling a PHP service--using an AJAX call--to get an RSS feed and display it in a DIV tag.
I have a PHP page that returns the RSS feed as a JSON array. I have confirmed that the service works, as I can run it from the browser and get appropriate output.
But when I run it from the AJAX call, I get success, but undefined response. I have tried putting a callback function, but no joy. I'm sure the solution is pretty simple.
Here is the PHP page (TestService.php)
<?php
header('Content-Type: application/json');
$url = $_REQUEST['sUrl'];
$year = $_REQUEST['getYear'];
$rss = simplexml_load_file($url);
$outlist = array();
//echo json_encode($outlist);
$i = 0;
foreach ($rss->channel->item as $item) {
$i = $i + 1;
$yr = date('Y', strtotime($item->pubDate));
if ($yr == $year && $i < 5) {
$outlist[] = array('title' => $item->title,
'pubDate' => $item->pubDate,
'description' => $item->description);
}
}
//$serializedList = serialized($outlist);
$encodedList = json_encode($outlist);
echo $encodedList;
?>
Here is the HTML (TestPage.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery RSS Demo</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<script>
$(document).ready(function () {
//set the initial URL call
var sNewsUrl = "http://rss.cnn.com/rss/cnn_topstories.rss?xml";
var today = new Date();
var curYear = today.getFullYear();
for (var yr = curYear; yr >= 2017; yr--) {
$('#SelectYear').append("<option>" + yr + "</option>");
}
$('#news').html();
$('#news').append("<li>" + sNewsUrl + "</li>");
$.ajax({
type: "POST",
url: "TestService.php",
contentType: "application/json; charset=utf-8",
data: "{ 'sUrl': '" + sNewsUrl + "', 'getYear': '" +
curYear + "' }",
dataType: "json",
success: function (response) {
var jsonData = JSON.parse(response.d);
$('#news').empty();
if (jsonData.length <= 0)
$('#divNews').hide();
else {
$('#divNews').show();
for (var i = 0; i < jsonData.length; i++) {
if (i < 3)
$('#news').append("<li>" +
jsonData[i].pubDate +
" <a href='" + jsonData[i].link
+ "' target='_blank'>" +
jsonData[i].title + "</a>" + "
</li>");
}
}
},
error: function (xhr, status, error) {
//debugger;
alert("Result: " + status + " " + error + " " +
xhr.status + " " + xhr.statusText)
}
});
});
<div id="divNews">
News:<br />
<ul id="news"></ul>
</div>
</body>
</html>

dblclick on marker not working at all

i am just doing my first experiences with maps api. Works great, but...
adding a click event to a marker works, adding a dblclick ... nothings happens. As if the dblclick simply doesnt fire.
So my questions:
Is a marker capable to handle a dblclick?
Sample code is as follows (the catch exception e doesnt make any change) :
try
{
if (urlfuerdoppelclick != null)
if (urlfuerdoppelclick.Length > 0)
{
eintraege = eintraege + " marker" + i.ToString() + ".addListener";
eintraege = eintraege + " ('dblclick',";
eintraege = eintraege + " function () ";
eintraege = eintraege + " { ";
eintraege = eintraege + " window.open(\"" + urlfuerdoppelclick
+ "\"); ";
eintraege = eintraege + " } ";
eintraege = eintraege + " ); ";
}
}
catch { };
try
{
if (voninnen != null)
if (voninnen.Length > 0)
{
eintraege = eintraege + " marker" + i.ToString() + ".addListener";
eintraege = eintraege + " ('click',";
eintraege = eintraege + " function () ";
eintraege = eintraege + " { ";
eintraege = eintraege + " window.open(\"" + voninnen + "\"); ";
eintraege = eintraege + " } ";
eintraege = eintraege + " ); ";
};
}
catch (Exception e) { };

SQL to search the entire table for a string

<!doctype html>
<html>
<title>Search</title>
<script type="text/javascript">
function query() {
var adVarWChar = 202;
var adParamInput = 1;
var pad = "C:\\Users\\azi!z\\Desktop\\Project\\Test.accdb";
var cn = new ActiveXObject("ADODB.Connection");
var strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pad;
cn.Open(strConn);
var cm = new ActiveXObject("ADODB.Command");
cm.ActiveConnection = cn;
cm.CommandText = "SELECT * FROM ImportFromExcel WHERE Module LIKE ? OR TestCase LIKE ? OR Openedby LIKE ? OR Status LIKE ?";
//cm.CommandText = "SELECT * FROM ImportFromExcel where TestCase LIKE ?";
cm.Parameters.Append(cm.CreateParameter(
"?",
adVarWChar,
adParamInput,
255,
"%" + document.getElementById("searchTerm").value + "%"));
var rs = cm.Execute(); // returns ADODB.Recordset object
if (rs.EOF) {
document.write("<p>No data found.</p>");
} else {
while (!rs.EOF) {
document.write("<p>" + rs.fields(0).value + ", ");
document.write(rs.fields(1).value + ", ");
document.write(rs.fields(2).value + ", ");
document.write(rs.fields(3).value + ", ");
document.write(rs.fields(4).value + ", ");
document.write(rs.fields(5).value + ", ");
document.write(rs.fields(6).value + ", ");
document.write(rs.fields(7).value + ", ");
document.write(rs.fields(8).value + ", ");
document.write(rs.fields(9).value + ", ");
document.write(rs.fields(10).value + ", ");
document.write(rs.fields(11).value + ", ");
document.write(rs.fields(12).value + ", ");
document.write(rs.fields(13).value + ", ");
document.write(rs.fields(14).value + ", ");
document.write(rs.fields(15).value + ".</p>");
var row = row.parentNode.rows[ ++idx ];
document.write(rs.fields(1).value + ".</p>");
rs.MoveNext();
}
}
rs.Close();
cn.Close();
}
</script>
</head>
<body>
<form method="get" name="SearchEngine" target="_blank">
<p style="text-align: center;"><span style="font-family:times new roman,times,serif;"><span style="font-size: 36px;"><strong>EA Search Engine</strong></span></span></p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"><input maxlength="300" id="searchTerm" name="KeywordSearch" size="100" type="text" value="Enter Your Keyword Here" /></p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"><input name="Search" type="button" value="Search" onclick="query();" /></p>
</form>
</body>
</html>
Please help me modify the SELECT statement in such a way that it covers the entire table (Not just the "TestCase" column) for ?.
Tried with changing the SELECT query to "SELECT * FROM ImportFromExcel WHERE col1 LIKE ? OR col2 LIKE ? OR col3 LIKE ?; ...", but it's not working.
If you want to search string in all columns of your table
you can concatenate all columns values and search value in the new column. For example
SELECT * FROM ImportFromExcel
WHERE Module
& column1
& column2
& column3 like '*substring*'

Why is Google login returning “immediate_failed” even when user logged somewhere else?

I am aware of similar questions but still have the problem:
"immediate_failed" - Could not automatially log in the user
Google login callback always shows "immediate_failed"
Google plus signin "immediate_failed" error
I also understand that the sign in callback is called initially even without request to check if the user is logged somewhere else. The “immediate_failed” is also returned correctly when the user is logged out within the browser from other Google services. However, when the user is in fact logged in Gmail in another Tab, I still receive the same failure in javascript.
This is the plain Google login code example. What could be wrong?. Some information:
Credentials:
Redirect URIs http://localhost:8000/beta/oauth2callback
Javascript Origins http://localhost:8000
Relevant Code (Javascript only sign in, copied and only slightly modified from: https://developers.google.com/+/web/signin/add-button)
Button declaration:
<div class="g-signin" data-callback="loginFinished"
data-clientid="268583......"
data-scope="profile email"
data-cookiepolicy="single_host_origin"
>
Callback:
var loginFinished = function(authResult) {
console.log(authResult)
if (authResult['code']) {
var el = document.getElementById('oauth2-results');
var label = '';
toggleDiv('oauth2-results');
if (authResult['status']['signed_in']) {
label = 'User granted access:';
gapi.auth.setToken(authResult);
} else {
label = 'Access denied: ' + authResult['error'];
}
el.innerHTML =
label + '<pre class="prettyprint"><code>' +
// ..
'}</code></pre>';
toggleDiv('signin-button');
} else {
document.getElementById('oauth2-results').innerHTML =
'Error';
}
};
Full code (served locally by Apache on :8000/test0/signin_demo_basic.htm)
<html>
<head>
<title>Google+ Sign-in button demo</title>
<style type="text/css">
html, body { margin: 0; padding:0;}
#signin-button {
padding: 5px;
}
#oauth2-results pre { margin: 0; padding:0;}
.hide { display: none;}
.show { display: block;}
</style>
<script type="text/javascript">
var loginFinished = function(authResult) {
console.log(authResult)
if (authResult['code']) {
var el = document.getElementById('oauth2-results');
var label = '';
toggleDiv('oauth2-results');
if (authResult['status']['signed_in']) {
label = 'User granted access:';
gapi.auth.setToken(authResult);
} else {
label = 'Access denied: ' + authResult['error'];
}
el.innerHTML =
label + '<pre class="prettyprint"><code>' +
// JSON.stringify doesn't work in IE8.
'{<br />' +
' "id_token" : "' + authResult['id_token'] +'",<br />' +
' "access_token" : "' + authResult['access_token'] + '",<br />' +
' "state" : "' + authResult['state'] + '",<br />' +
' "expires_in" : "' + authResult['expires_in'] + '",<br />' +
' "error" : "' + authResult['error'] + '",<br />' +
' "error_description" : "' + authResult['error_description'] + '",<br />' +
' "authUser" : "' + authResult['authuser'] + '",<br />' +
' "status" : {"' + '<br />' +
' "google_logged_in" : "' + authResult['status']['google_logged_in'] + '",<br />' +
' "method" : "' + authResult['status']['method'] + '",<br />' +
' "signed_in" : "' + authResult['status']['signed_in'] + '"<br />' +
' }<br />' +
'}</code></pre>';
toggleDiv('signin-button');
} else {
document.getElementById('oauth2-results').innerHTML =
'Error';
}
};
function toggleDiv(id) {
var div = document.getElementById(id);
if (div.getAttribute('class') == 'hide') {
div.setAttribute('class', 'show');
} else {
div.setAttribute('class', 'hide');
}
}
</script>
<script src="https://plus.google.com/js/client:platform.js" type="text/javascript"></script>
</head>
<body>
<div id="signin-button" class="show">
<div class="g-signin" data-callback="loginFinished"
data-clientid="268583......"
data-scope="profile email"
data-cookiepolicy="single_host_origin"
>
</div>
</div>
<div id="oauth2-results" class="hide"></div>
<div>Reload the example or open in a new window</div>
</body>
</html>
After painfully checking my code and looking for answers I found out that in my Firefox the option to accept third party cookies was disabled. To solve the problem, in Firefox go to Options > Privacy and set "accept third party cookies" to visited. Please inform yourself about what accepting third party cookies implies.
For anybody who has a problem only on IE, set the immediate key within window.gapi.auth.authorize to false and try it out.