Why do object responses undefined? - javascript

<script src="http://vkontakte.ru/js/api/openapi.js" type="text/javascript">
</script>
<div id="login_button" onclick="VK.Auth.login(getit);"></div>
<script language="javascript">
VK.init({
apiId: 2903251
});
function getit(response) {
if (response.session) {
var id = response.session.mid;
}
VK.Api.call('users.get', {uids: id, fields: 'sex,photo_big'}, function(r) {
if(r.response) {
alert(r.response.sex);
console.log(r.response);
}
});
}
VK.UI.button('login_button');
</script>
Why does alert(r.response.sex) display undefined, but everything is OK with console logs?

Solved. The solution:
r.response[0].sex

Related

Linkedin login with js

I am trying to implement login with linked in my web app. Here is my code..
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="//platform.linkedin.com/in.js">
api_key: 'key here'
</script>
</head>
<body>
<script>
IN.User.authorize(function (e) {
console.log(e);
}, function (e) {
console.log(e);
});
</script>
</body>
But its giving me error 'Cannot read property 'then' of undefined at Object.authorize (in.js:18)'
i have also tried this way..
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="//platform.linkedin.com/in.js">
api_key: 'key here'
onLoad: 'onLinkedInLoad'
authorize: true
</script>
</head>
<body>
<!-- LinkedIn signin button -->
<script type="in/Login"></script>
<script type="text/javascript">
// Setup an event listener to make an API call once auth is complete
function onLinkedInLoad() {
IN.Event.on(IN, "auth", getProfileData);
}
</script>
</body>
but its giving me errror
'in.js:7 Uncaught (in promise) TypeError: Could not instantiate tag for 'login': Cannot read property 'on' of null
at new (Login.js?version=0.1.149:7)
at in.js:18'
The documentation and examples for linkedIn show api_key and onLoad function not being set as a string delimeter.
api_key: 0192afsdj10
onLoad: onLinkedInLoad
Secondly, you have not added in a function for the getProfileData function.
Here is an example of the should look like.
function getProfileData() {
IN.API.Profile("me").fields("id", "first-name", "last-name", "headline", "location", "picture-url", "public-profile-url", "email-address").result(displayProfileData);
}
function displayProfileData(profiles){
var member = profiles.values[0];
console.log(member);
}
See code referenced here

File_get_contents and echo with javascript

I'm not familiar with js. I'd like to get the result of "file_get_contents" function and put it on the "source" (I have marked both with "https://.................).
Thank you in advance.
<script>
var myInit = {
referrer: '',
};
function file_get_contents(filename) {
fetch(filename, myInit).then((resp) => resp.text()).then(function(data) {
content = JSON.parse(data)
fetch(content['some']['media']['content'], myInit).then((resp) =>
resp.text()).then(function(data));});}
file_get_contents("https://.................");
</script>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/clappr#latest/dist/clappr.min.js">
</script>
</head>
<div id="player"></div>
<script>
window.onload = function() {
var player = new Clappr.Player({
source: 'http://......................',
parentId: "#player",
height: '100%',
width: '100%',
autoPlay: true,
});};
</script>
</body>
</html>

Create javascript function for submit button to return data

I'm creating a blog on laravel and so far I have the successful js code for posts that contain a title and content. But I'm having some trouble writing the js function for tags.
I would like to do the same for tags but I'm getting errors on everything I try.
<script src="https://cloud.tinymce.com/stable/tinymce.min.js?apiKey=fg5tc8gb4rtw6p9n3njd2hi4965rketxda84pbcfs09hb5x2"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.6.4/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
selector: '.myeditablediv',
plugins: 'code , save, autoresize , textcolor colorpicker , emoticons, textpattern , wordcount',
toolbar: 'save , restoredraft , forecolor backcolor, emoticons',
save_onsavecallback: function () {
var content = tinymce.activeEditor.getContent();
console.log(content);
}
});
$(document).on('click', '#SubmitBtn', function () {
var content = tinymce.activeEditor.getContent();
var data = {
'title': $('#title').val(),
'content': content,
'_token': '{{csrf_token()}}'
};
$.post('/postData', data, function () {
console.log(data);
});
});
</script>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Create the title</h1>
<form>
{{csrf_field()}}
<label for="title">Click here to edit the title of your post!</label>
<input type="text" name="title" id="title"/>
<h1>Create the content</h1>
<div class="myeditablediv">Click here to edit the content of your post!</div>
</form>
<input type="button" name="Submit" id="SubmitBtn" value="Submit"/>
</body>
</html>
<script src="https://cloud.tinymce.com/stable/tinymce.min.js?apiKey=fg5tc8gb4rtw6p9n3njd2hi4965rketxda84pbcfs09hb5x2"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.6.4/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
selector: '.myeditabletag', // change this value according to your HTML
menu: {
view: {title: 'Edit', items: 'cut, copy, paste'}
},
save_onsavecallback: function () {
var content = tinymce.activeEditor.getContent();
console.log(content);
}
});
$(document).on('click', '#SubmitBtn', function () {
var name = tinymce.activeEditor.getContent();
var data = {
'name': name,
'_token': '{{csrf_token()}}'
};
$.post('/postTags', data, function () {
console.log(data);
});
});
</script>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Create a new Tag</h1>
<form>
{{csrf_field()}}
{{--<input type="text" name="name" id="name"/>--}}
<div class="myeditabletag">Click here to edit the name of your tag!</div>
</form>
<input type="button" name="Submit" id="SubmitBtn" value="Submit"/>
</body>
</html>
Here is the route for /postData for tags and posts:
Route::post('/postTags', ['uses' => 'TagController#store']);
Route::post('/postData', ['uses' => 'PostController#store']);
And here is the PostController and TagController store method:
public function store(Request $request)
{
$post = new Post;
$post->title = $request['title'];
$post->content = $request['content'];
$post->save();
}
public function store(Request $request)
{
$tag = new Tag;
$tag->name = $request['name'];
$tag->save();
}
You have a mistake in your JS code. You are selecting an ID that doesn't exist. You need to select the content of the changed tag, and send that as the data['name']. Try the following code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.6.4/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
selector: '.myeditabletag', // change this value according to your HTML
menu: {
view: {title: 'Edit', items: 'cut, copy, paste'}
},
save_onsavecallback: function () {
var content = tinymce.activeEditor.getContent();
console.log(content);
}
});
$(document).on('click', '#SubmitBtn', function () {
var name = tinymce.activeEditor.getContent();
var data = {
'name': name,
'_token': '{{csrf_token()}}'
};
console.log(data);
$.post('/postTags', data, function () {
});
});
</script>

Passing input data from view to controller

Hello I'm new to MVC and AJAX.
I have a problem sending input data from view to controller.
What is the wrong thing in this code ? :
Thanks in advance.
Head :
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
View :
<div id="inputdiv">
<p>Display name: <input type="text" id="name1" /> </p>
<p><button id="submitbutton">Submit</button></p>
</div>
<script type="text/javascript" language="javascript">
$(function () {
$("#submitbutton").click(function () {
var nameEntered = $("#name1").val()
$.post(
'<%= Url.Action("SimplePost") %>',
{ submittedName: nameEntered },
handleSuccess
);
});
});
function handleSuccess(content) {
$("#inputdiv").html(content);
$("#inputdiv").addClass("easy-notification");
}
</script>
Controller :
[HttpPost]
public ContentResult SimplePost(string submittedName)
{
string result = string.Format("Hello {0}!?", submittedName);
return new ContentResult { Content = result };
}

Syntax Error in Json while fetching in backbone.js

I am trying to fetch the data from the JSON, and trying to display it. While fetching the json, it is throwing error in fetch. I am stuck at this point from so many days, and after reading from google, I am confused. Can anyone please help me out what is the error and how to proceed.
<!DOCTYPE html>
<html>
<head>
<title>Fortified Studio</title>
</head>
<body>test
<div id="profiles"></div>
<script id="profileTemplate" type="text/template">
<div class="profile">
<div class="info">
<div class="name">
<%= name %>
</div>
</div>
</div>
</script>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script>
$(function() {
console.log("start of script");
var Prof = Backbone.Model.extend({});
var ProfList= Backbone.Collection.extend ({
model: Prof,
url : '/data/testjson2.json'
});
var ProfView = Backbone.View.extend ({
initialize: function (){
console.log("View initialize");
this.render();
},
render : function (){
console.log("in render");
template: _.template($('#profileTemplate').html()),
_.each (this.collection, function(Prof) {
var profileTemplate = this.template(Prof.toJSON());
$(this.el).append(profileTemplate);
}, this);
console.log (this);
return this;
}
});
var profs = new ProfList ();
var profViews = new ProfView ();
new ProfList().fetch({
sucess : function(){
console.log('json loaded');
},
error: function (){
console.log("error retrieving model");
}
});
profViews.render();
});
</script>
</html>
and my JSON is:-
[
{
"name": "Johny Johny",
},
{
"name": "Jack n Jill",
}
]
and output on console is:-
.......
Unknown property 'box-sizing'. Declaration dropped. myFile.html
"start of script" myFile.html:27
"View initialize" myFile.html:37
"in render" myFile.html:41
[object Object] myFile.html:47
"in render" myFile.html:41
[object Object] myFile.html:47
GET /data/testjson2.json [HTTP/1.1 200 OK 3ms]
syntax error testjson2.json:1
"error retrieving model" myFile.html:62
Please help me out, how to proceed.
Remove the commas at the end of each value set.
[
{
"name":"Johny Johny"
},
{
"name":"Jack n Jill"
}
]
Test your JSON with:
http://jsonformatter.curiousconcept.com/

Categories