how to redirect page in html? - javascript

i am writing page in html and i have server/database on parse.com .
parse provides login function which checks if username and password is matched in a database. i have done this part but when i get success it is not redirecting into other page. here is the code :
$("#login").click(function(event)
var name = $(#name).val();
var pass = $(#password).val();
Parse.User.logIn(name, pass, {
debugger
success: function(user){
window.location="login/login.html";
console.log("everything OK")
}, error: function(user, error){
console.log("Log in Error:"+error.message);
}
});
});
i have searched for redirecting the page and found out the window.location but it is not working. what is the error please help me.
By the way i got error message at this line var name = $(#name).val(); with error message :
Uncaught SyntaxError: Unexpected token ILLEGAL

You have a couple of problems in your code which is stopping it from running. Unexpected token ILLEGAL is the browser's way of telling you that it doesn't understand your code.
1) You're missing the opening brace in the first function
2) In the jQuery selectors, you need to wrap the selectors in quotes - $("#name").val() and $("#password").val()
3) Your debuger statement is misplaced. You've put it inside an object definition, and it doesn't make sense there. Move it to inside the function below. (It's also spelt debugger with 2 g's :) )
$("#login").click(function(event) { // 1) include brace
var name = $("#name").val(); // 2) include quotes
var pass = $("#password").val();
Parse.User.logIn(name, pass, {
success: function(user){
debugger; // 3) move debugger statement to a valid location
window.location="login/login.html";
console.log("everything OK")
},
error: function(user, error) {
console.log("Log in Error:"+error.message);
}
});
});

You need to pass both of your selectors in quotes
var name = $("#name").val();
var pass = $("#password").val();
That will stop the error and should work.
This is riddled with errors and missing semicolons and parantheses. Try this
$("#login").click(function(event) {
var name = $("#name").val();
var pass = $("#password").val();
Parse.User.logIn(name, pass, debuger);
success: function(user){
window.location="login/login.html";
console.log("everything OK")
}, error: function(user, error){
console.log("Log in Error:"+error.message);
}
});
});

Related

Passing a variable from javascript to Django view

I want to send a variable I got from a javascript function to my Django view. The function is as below:
<script>
function googleSignin() {
firebase.auth().signInWithPopup(provider).then(function(result) {
var token = result.credential.accessToken;
var user = result.user;
console.log(token)
console.log(user)
var uid = user.uid;
return uid;
}).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(error.code)
console.log(error.message)
});
}
</script>
I used a onclick tag to call this function and the values properly show up in the console. I just need to send the value returned to my views somehow. I've tried using ajax to call on the view but I can't figure out how to make it work. The ajax code is below:
$.ajax({
// points to the url where your data will be posted
url: /gsignin/,
// post for security reason
type: "POST",
// data that you will like to return
data: {uid : uid},
// what to do when the call is success
success:function(response){
console.log(sid);
},
// what to do when the call is complete ( you can right your clean from code here)
complete:function(){
window.location.href = "/gsignin/"
},
// what to do when there is an error
error:function (xhr, textStatus, thrownError){}
});
I'm really new to this stuff but I can't find any proper documentation on implementing this ajax. Also it is not necessary to use the ajax jquery at all, I just need to pass the returned variable from the function.

JQuery.post Failed to execute 'postMessage'

I have a simple $.post() that I want to execute
function(formData) {
var path = reg ? "login" : "regester";
formData.bundle_id = window.bundleId;
app.error = false;
app.unknown = false;
$.post(("/kanban/"+path+"/submit"),{"form":formData},function(data){
if(data.success){
window.location.href=data.redir;
}
else if(data.user) {
window.location.href="/kanban";
}
else {
app.error = true;
}
})
.fail(function(jqXHR, textStatus, errorThrown) {
app.unkown = true;
});
}
The app is defined by
const app = new Vue({...});
Currently, when the function is called I get the error
jquery.js:8463 Uncaught TypeError: Failed to execute 'postMessage' on 'Window': 2 arguments required, but only 0 present.
at add (jquery.js:8463)
at buildParams (jquery.js:8450)
at buildParams (jquery.js:8444)
at Function.jQuery.param (jquery.js:8483)
at Function.ajax (jquery.js:9073)
at Function.jQuery.(/kanban/anonymous function) [as post] (https://127.0.0.1/kanban/js/jquery.js:9355:17)
at submitForm (register?bundle_id=-1:84)
at <anonymous>:1:1
I'm not sure what could be causing this. It may be a simple syntax error but I can't find one.
I call my js method from here
<form action="javascript:submitForm(this)">
......
</form>
I'm running on Google Chrome Version 69.0.3497.81 With jquery-3.3.1
I also have vue.js and alertify.js loaded on this page.
Is there a syntax problem I'm missing? Or is there a possible overlap.
You are trying to pass a form element to $.post as data and that can't be serialized.
Try changing to
var data = $(formData).serialize()
$.post("/kanban/"+path+"/submit", data, function(data){..
Now server will receive the data exactly the same way it would if the form was submitted using default browser process
Would also consider changing name of formData to form to minimize confusion

What's missing in my Sign In request?

I'm trying to access the endpoints of a website (lighterpack.com, github: https://github.com/galenmaly/lighterpack). To test my ability to do this, I'm trying to make a POST call to the 'signin' function in edit.js (bottom).
I'm using Postman to send the POST request, sending it the object {username: myusername, password: myhashedpasswordandusername}
I'm hashing the object manually right now on my local machine, and copying that out to Postman. I created a JS file with the "crypto-js" dependency, and ran the command that the edit.js file uses, after setting var CryptoJS = require('crypto-js').
This might be the part I messed up, but I can't see where. I
My hashing file code:
var crypto = require('crypto-js')
var pass = "mypassword"
var name = "username"
var hash = crypto.SHA3(pass+name)
var hash= hash.toString(crypto.enc.Base64)
console.log(hash)
I take what the console logs out, and put that in my Postman object as my password. The thing I don't get is that in the edit.js file, CryptoJS doesn't appear defined anywhere, not that I could see anyway. Can someone point me in the right direction?
Original edit.js function:
$(".signin").on("submit", function(evt) {
evt.preventDefault();
var form = this;
var error = "";
var username = $(".username", this).val();
var password = $(".password", this).val();
if (!password) error = "Please enter a password.";
if (!username) error = "Please enter a username.";
if (error) {
$(".lpError", this).text(error).show();
return;
}
$(".lpError", this).text("").hide();
username = username.toLowerCase();
var hash = CryptoJS.SHA3(password+username);
hash = hash.toString(CryptoJS.enc.Base64);
$.ajax({
url: "/signin",
data: {username: username, password: hash, },
method: "POST",
error: function(data, textStatus, jqXHR) {
var error = "An error occurred.";
if (data.responseText) error = data.responseText;
$(".password", form).val("").focus();
$(".lpError", form).text(error).show();
},
success: function(data) {
$("#signin, #lpModalOverlay").fadeOut("slow", removeBlackout);
$(".password, .username", form).val("");
signedIn(data.username);
library.load(JSON.parse(data.library));
initWithLibrary();
}
});
});
So someone got me the answer. In github, there is a file public/sha3.js that has the CryptoJS variable, and the app.js file declares that file's info (CryptoJS) as a global variable.
I copied sha3.js, and was able to correctly hash my data and then login. Success!

Parse Cloudcode query error 141

If I run the Parse JavaScript code below locally, the query works fine. However, when I adapt the code to run as Parse cloudcode, the same query is not executed, and when I test the query content I get an error with code 141.
I have tested both functions at various points. The parameter comp_id and request.params.comp_id are the same.
Parse JavaScript query (run locally, works fine)
function testOne(comp_id) {
Parse.initialize(*****,*****);
var Competition = Parse.Object.extend("Competition");
var query = new Parse.Query(Competition);
console.log(query); // <~ gets a valid query
query.get(comp_id, {
success: function(competition) {
console.log(competition.id);
},
error: function(competition, error) {console.log(error);}
});
}
Parse cloudcode query (query does not execute, error 141)
Parse.Cloud.define("testOneCloud", function(request, response) {
var comp_id = request.params.comp_id;
var Competition = Parse.Object.extend("Competition");
var query = new Parse.Query(Competition);
msg = query; // <~ gets undefined error 141
query.get(comp_id, {
success: function(competition) {
msg = competition.id; // <~query success code does not get executed
},
error: function(competition, error) {console.log(error);}
});
response.success(msg);
});
Any idea why one of these two seemingly identical Parse programs work and the other doesn't?
It seems the undeclared msg variable is causing the problem. How about (eliminating the extra variable and using promises):
Parse.Cloud.define("testOneCloud", function(request, response) {
var Competition = Parse.Object.extend("Competition");
var query = new Parse.Query(Competition);
query.get(request.params.comp_id).then(function(competition) {
response.success(competition.id);
}, function(error) {
response.error(error);
});
});

what does Uncaught SyntaxError: Unexpected token < mean?

For this line of code :
var result = eval('('+result+')');
In this context:
function saveUser(){
alert(url);
$('#fm').form('submit',{
url: url,
onSubmit: function(){
return $(this).form('validate');
},
success: function(result){
var result = eval('('+result+')');
if (result.errorMsg){
$.messager.show({
title: 'Error',
msg: result.errorMsg
});
} else {
$('#dlg').dialog('close'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
}
}
});
}
How do i fix the error?
what does Uncaught SyntaxError ... mean?
It means that eval cannot parse the input (as JavaScript) because it contains a < where there shouldn't be one. FWIW if the response is HTML, JSON.parse wouldn't help either.
How do i fix the error?
You either have to treat the response how it is expected to be treated, e.g. don't pass it through eval if it's HTML.
Or you fix the server side and return the repsonse that the client side expects, e.g. JSON.

Categories

Resources