Lexik Bundle Symfony 3 - html

I use LexikAuthentication Bundle. When I try to make the UI using ajax for login_check, I get back the token in localStorage but when I try to login to my UI, I get 401 not authorization.
This is my login_check:
function authenticate(e) {
e.preventDefault();
$.ajax({
url: 'http://localhost:8000/login_check',
type: 'POST',
data: JSON.stringify({
'_username': $('#username').val(),
'_password': $('#password').val()
}),
dataType: 'json',
contentType: "application/json",
access_token: "",
crossDomain: true,
success: function (data) {
if (data != null && data != '') {
var token = "Bearer " + data.token;
console.log(token);
localStorage.setItem('token', data.token);
}
}
});
Anyone know how to fix this?

Okay i found the solution by my self.
The error was Here : localStorage.setItem('token', data.token);
It should be like this : localStorage.setItem("Bearer " ,data.token);

Related

Website Redirect Code Error, how do I fix it?

Can anyone see why this script would fail to redirect?
$.ajax({
url: url,
data: JSON.stringify(viewModel),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
cache: false,
type: "POST",
success: function (result) {
if (result.success === true) {
showFullScreenLoadMask(false);
//var redirect = "/Login/Index";
//window.location.href = getUrlPath() + redirect;
var redirecturl = getUrlPath() + "/Login/Index";
/*F5_*/ F5_Deflate_location( /*_5F##*/ window /*F5_*/ ) /*_5F#.location#*/ .assign = redirecturl;
} else {
// hide any previous errors
$("#divPasswordHistoryError").hide();
$("#divFullNameError").hide();
$("#divCurrentPasswordError").hide();
if (result.vm.ShowPreviousPasswordFail === true) {
$("#divPasswordHistoryError").show();
$("#divPasswordHistoryError").removeClass("hidden");
}
if (result.vm.ShowFullNameError === true) {
$("#divFullNameError").show();
$("#divFullNameError").removeClass("hidden");
}
if (result.vm.ShowCurrentPasswordFail === true) {
$("#divCurrentPasswordError").show();
$("#divCurrentPasswordError").removeClass("hidden");
}
$("#Username").val(result.vm.Username);
$("#Password").val(result.vm.Password);
$("#NewPassword").val("");
}
},
error: function (responseText, textStatus, errorThrown) {
alert('Error - ' + errorThrown);
}
});
Above is the code I have on my login script, but for the life of me, I cant get my head around as to why it would not be redirecting to the correct page after someone enters their password.

flask-cors is not parsing the data from ajax client

I am using flask for creating some endpoints for a blockchain project . I need to accept json data from ajax client . Since it is cros platform , i am using flask cors . But i cant seem to find a solution. It is not working
I have already tried doing
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app, origin = '*')
Basically my client code is as follows .
$.ajax({
url: 'http://localhost:8080/ratings/new',
dataType: 'json',
type: 'POST',
contentType: 'application/json',
data: json1,
crossDomain: true,
xhrFields: {
withCredentials: true
},
processData: false,
beforeSend: function (xhr) {
//xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
},
success: function (data, textStatus, jQxhr)
{
$('body').append(data);
console.log(data);
},
error: function (jqXhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
And at my server i have an endpoint of
#app.route('/ratings/new', methods = ['POST','OPTIONS'])
def rating():
values = request.get_json()
if values == None:
return "No data received", 400
#ratings = values['rating']
index = blockchain.new_ratings(values)
response = {
'Message': f'New transaction will be added to the block {index}',
}
response = jsonify(response)
response.headers.add('Access-Control-Allow-Origin','*')
return response, 201
At the server side i am not receiving the required data and at the client side i am getting the following error .
Access to XMLHttpRequest at 'http://localhost:8080/ratings/new' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Please help me solve this problem . Thanks in advance.
Silly mistake , make the withCredentials: false
$.ajax({
url: 'http://localhost:8080/ratings/new',
dataType: 'json',
type: 'POST',
contentType: 'application/json',
data: json1,
crossDomain: true,
xhrFields: {
withCredentials: false
},
processData: false,
beforeSend: function (xhr) {
//xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
},
success: function (data, textStatus, jQxhr)
{
$('body').append(data);
console.log(data);
},
error: function (jqXhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});

Laravel - Ajax error: xhr.send( options.hasContent && options.data || null );

When I refresh the document, sometimes is working fine (I get the json object in console), but many times I get 500 (Internal Server Error) and from jQuery I see the error is: xhr.send( options.hasContent && options.data || null ); Please help me. Here is the code:
$(document).ready(function(){
$.ajax({
url: '/ajax/cis',
method: "GET",
success: function(data){
console.log(data);
}
});
});
'/ajax/cis' is a laravel controller wich only returns a json object:
return response()->json(['cis_names'=>'cis_names']);
I also tried with post method:
$.ajax({
url: '/ajax/cis',
method: "POST",
data:{_token: token},
//var token = Session::token()
success: function(data){
console.log(data);
}
});
Try this with using Blade and see if that works:
$.ajax({
url: '/ajax/cis',
method: "POST",
data:{_token: '{{csrf_token()}}'},
success: function(data){
console.log(data);
}
});

Chrome extension transfer to manifest 2 - No ajax response

Hayush!
Been trying to transfer my Chrome extension to manifest 2. Everything is working except for one script which calls an ajax. I have a variable that contains a JSON content which needs to be transferred to the server.
function sendlist(list){
jsontext = JSON.stringify(list);
$.ajax({
url: amfurl + "user/listbookmarks/",
dataType: 'text json',
async: true,
type: 'POST',
processData: false,
data: {'folders': jsontext},
success: function(data){
$('#importing').css('display','none');
$('#importdone').css('display','block');
console.log(data);
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}
For some reason the ajax part is not executed at all. No error message was triggered, nor in the server error log.
All of the inline scripts and scripts in general were included in the popup.js. So it's probably not the issue.
Any thoughts?
Thanks!
The following code works perfectly on previous manifest
function importbookmarks(){
$('#formadd').css('display','none');
$('#importing').css('display','block');
_gaq.push(['_trackEvent', 'chromextension','importbookmarks', username]);
chrome.bookmarks.getTree(function(bookmarks) {
sendlist(bookmarks);
});
}
function sendlist(list){
jsontext = JSON.stringify(list);
$.ajax({
url: amfurl + "user/listbookmarks/",
dataType: 'text json',
async: true,
type: 'POST',
// processData: false,
data: {'folders': jsontext},
success: function(data){
$('#importing').css('display','none');
$('#importdone').css('display','block');
console.log(data);
}
});
}
The problem wasn't with the function. The button execution wasn't calling it correctly.
Here is the code I used to get the function to work in manifest 2
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("import-bookmarks").addEventListener("click", function () {
importbook();return;
});

JSON data not parsing in node.js displays undefined in console

JSON data not parsing in node.js displays undefined in console..
Here is HTML code:
var jsonObjects = [{id:1, name:"amit"}];
jQuery.ajax({
url: 'http://localhost:8081',
type: "POST",
data: {"id":"1", "name":"amit"},
dataType: "json",
success: function(result) {
alert("done")
}
});
Here is Nodejs code:
http.createServer(function (request, response)
{
response.writeHead(200, {"Content-Type":"text/plain"});
var urlObj = JSON.stringify(response.data, true);
console.log(urlObj)
response.end();
}).listen(8081);
Try using GET method Instead of Post. Try this
var jsonObjects = [{id:1, name:"amit"}];
$.ajax({
type: 'GET',
url: 'http://localhost:8081',
data: {
jsonData: JSON.stringify(jsonObjects)
},
dataType: 'json',
complete: function(validationResponse) {
}
});
The data will be held in the request, not the response as it has come from the client request.
If you are using express to create your http server you will also need to tell it to use the bodyParser middle wear.