Loading recaptcha asynchronously (grecaptcha.enterprise) - recaptcha

I'm using grecaptcha.enterprise v3 but it is common for some requests to have connection failures.
I want to load the async script but in enterprise mode.
<script src="https://www.google.com/recaptcha/enterprise.js?badge=<?php echo $badge; ?>&size=invisible&render=<?php echo RECAPTCHA_SITE_KEY; ?>"></script>
<script>
function recaptchaRenew() {
grecaptcha.enterprise.execute('<?php echo RECAPTCHA_SITE_KEY; ?>', {action:'validate_captcha'})
.then(function(token) {
console.log('reCaptcha');
// Apply token
document.querySelector('.js-g-recaptcha-token').value = token;
});
}
function recaptchaSubmit(token) {
var $form = document.querySelector('.js-recaptcha');
if ($form) {
$form.submit();
}
}
grecaptcha.enterprise.ready(function(){
recaptchaRenew();
});
</script>

Related

How can we use several google recaptchas in the same page?

When i put
<script>
var renderCaptchas = function() {
grecaptcha.render(this, {
'sitekey' : {{ google_recaptcha_site_key }}
});
};
var onloadCaptchaCallback = function () {
Array.prototype.forEach.call(document.querySelectorAll('.g-recaptcha'),
renderCaptchas);
};
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCaptchaCallback&render=explicit" async defer></script>
I have this error :
reCAPTCHA couldn't find user-provided function: onloadCaptchaCallback
The function is yet well defined ...
If i remove
grecaptcha.render(this, {
'sitekey' : {{ google_recaptcha_site_key }}
});
Then it finds the callback ...
I use the Google ReCaptcha v2, with the checkbox "mode".
There is something strange in your loop for me and grecaptcha.render(this !? this = renderCaptchas ... no !? So the element you want to render is not good because it is not a valid html container.
<script>
var renderCaptchas = function(element) {
grecaptcha.render(element, {
'sitekey' : 'google_recaptcha_site_key'
});
};
</script>
or something like :
<script>
var onloadCaptchaCallback = function () {
[].forEach.call(document.querySelectorAll('.g-recaptcha'), function(element){
grecaptcha.render(element, {
'sitekey' : 'google_recaptcha_site_key'
});
}
});
};
</script>
How to loop over document.querySelectorAll :
How to loop through selected elements with document.querySelectorAll

0 value is returned on success result in ajax in wordpress

JS
<script type="text/javascript">
function myfunction(course_id, subject_id) {
var site_url = '<?php echo site_url(); ?>';
var ajaxurl = site_url + "/wp-admin/admin-ajax.php";
alert(ajaxurl);
var data = {
action: 'example_ajax_request'
};
jQuery.post(ajaxurl, data, function(response) {
alert(response);
});
}
</script>
PHP
add_action('wp_ajax_example_ajax_request', 'insert_data_customtable' );
add_action('wp_ajax_nopriv_example_ajax_request','insert_data_customtable');
function insert_data_customtable()
{
$result="hi";
echo $result;
exit(0);
}
Can you elaborate the question a little bit. You have passed parameters to functions where are from they coming.
echo json_encode($result);
echo $result;
it will display the value stored in it

Post with AngularJS doesn't work

I would like to send a post request to my API. It works with jQuery :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$.ajax({
type: "POST",
url: "api.php?option=inscription",
data: {lol : "mess"}
});
</script>
But it doesn't with AngularJS :
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
{{1+1}}
<script>
$http.post('api.php?option=inscription', {lol : "mess2"})
.success(function(){alert('cool');});
</script>
If someone can help me. Thank you !
UPDATE :
Thank for your answers, I wanted to simplify but it wasn't clear anymore. So with your help, this is my new code, and the problem is the same. The data in the backend is empty ;
frontend :
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
<div ng-controller="MainCtrl"></div>
{{data}}
<script>
var app = angular.module('myApp', []);
app.service('SomeService', function($http) {
this.readData = function(dataUrl, dataTobePosted) {
var back = $http.post(dataUrl, dataTobePosted);
back.success(function(data){
console.log(data);
return data;
}).error(function(data, status, headers, config) {
return status;
});
}
});
app.controller('MainCtrl', function($scope, $http, SomeService){
$scope.readData = function(url) {
var dataTobePosted = {"lol": "mess"};
$scope.data = SomeService.readData(url, dataTobePosted);
}
$scope.readData('api.php?option=inscription');
});
</script>
</html>
For clarity, I am suggesting a simple implementation. However, further reading may needed in order to understand the behaviour precisely.
angular.module('myApp').service('SomeService', function($http) {
this.readData = function(dataUrl, dataTobePosted) {
// read data;
return $http.post(dataUrl, dataTobePosted)
.then(function(res) {
return res.data;
}, function(res) {
return res;
}
}
return this;
});
angular.module('myApp').controller('MyController', function($scope, SomeService) {
$scope.readData = function(url) {
var dataTobePosted = {"lol": "mess"};
SomeService.readData(url, dataTobePosted)
.then(function(res) {
$scope.data = res;
}, function(res) {
// Display error
}
}
$scope.readData('api.php?option=inscription');
}
Usage in the HTML page
<div ng-controller="MyController">
{{data}}
</div>
You're using AngularJS as if it's jQuery. It's not. AngularJS works with dependency injection, so you need to wrap your $http call inside a controller.
You should probably read up on AngularJS. A few useful links:
https://docs.angularjs.org/guide/introduction
https://docs.angularjs.org/guide/controller
https://docs.angularjs.org/guide/di
"Thinking in AngularJS" if I have a jQuery background?
My bad, my problem came from my backend in the php I just get my data with :
$data = json_decode(file_get_contents("php://input"));
and not with $_POST

Marionette events are not triggered on Route

Why Marionette events are not triggered when I access a URL?
When I access the URL it suppose to call the function API.goHome()
but it doesn't! I don't understand why?
Here is my Code:
App.js
var PatientPortal = new Marionette.Application();
PatientPortal.addRegions({
'headerRegion': '#header',
'bodyRegion': '#body',
'footerRegion': '#footer'
});
PatientPortal.on("before:start", function () {
console.log("Started");
});
PatientPortal.navigate = function (route, options) {
options || (options = {});
Backbone.history.navigate(route, options);
};
PatientPortal.getCurrentRoute = function () {
return Backbone.history.fragment
};
PatientPortal.on("start", function(){
if (Backbone.history) {
Backbone.history.start();
}
if(PatientPortal.getCurrentRoute() == ""){
PatientPortal.navigate('home');
}
});
PatientPortal.start();
and Router Code:
PatientPortal.module("Portal", function (Portal, PatientPortal, Backbone, Marionette, $, _) {
Portal.Router = Backbone.Marionette.AppRouter.extend({
controller: "API",
appRoutes: {
"": "goHome",
"home": "goHome"
}
});
var API = {
goHome: function () {
console.log("go home");
}
};
PatientPortal.on("home:route", function () {
console.log("OKOKOK")
API.goHome();
});
PatientPortal.addInitializer(function () {
new Portal.Router({
controller: API
});
});
});
and here a home page code:
<!DOCTYPE html>
<html>
<head>
<title>INSAH - Login Page</title>
</head>
<body>
<div id="header"></div>
<script src="./js/assets/provider/jquery/jquery-2.1.1.min.js"></script>
<script src="./js/assets/provider/underscore/underscore-min.js"></script>
<script src="./js/assets/provider/backbone/backbone-min.js"></script>
<script src="./js/assets/provider/backbone/backbone.babysitter.min.js"></script>
<script src="./js/assets/provider/backbone/backbone.wreqr.min.js"></script>
<script src="./js/assets/provider/marionette/backbone.marionette.min.js"></script>
<script src="./js/app.js"></script>
<script src="./js/routes/route.js"></script>
</body>
</html>
Thanks.
I figured out the problem, it was because of starting the backbone history, the AddInitializer functuion shoudl be as following:
PatientPortal.addInitializer(function () {
new Portal.Router({
controller: API
});
Backbone.history.start();
});
and we should remove this line from app.js:
Backbone.history.start();

socket.io not working in Chrome

i'm trying the this simple socket.io example and it works in Safari (send/receive from both sides). However, in the Chrome the client receives messages but the server won't receive messages sent by the client
index.html
<!doctype html>
<html>
<head>
<title>web sockets</title>
<meta charset="utf-8">
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8888');
socket.on('news', function (data) {
console.log(data);
writeMessage(data);
socket.emit('my other event', { my: 'data' });
});
function writeMessage(msg) {
var msgArea = document.getElementById("msgArea");
if (typeof msg == "object") {
msgArea.innerHTML = msg.hello;
}
else {
msgArea.innerHTML = msg;
}
}
</script>
</head>
<body>
<div id="msgArea">
</div>
</body>
</html>
server.js
var app = require('http').createServer(handler)
, io = require('/usr/local/lib/node_modules/socket.io').listen(app)
, fs = require('fs')
app.listen(8888);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Am I missing something? Chrome is on v.19
You can use http://socket.io-test.com to see if the problem is with your browser/proxy or your code.

Resources