javascript cookie, Ember - javascript

I want to put a user input into a cookie to save the value for later display. It seems I can write into the cookie, but not able to get the value back. I have this simple test set up:
var test = "TEST";
document.cookie = "usr=TEST;";
$.cookie("usr", test);
Ember.$.cookie('usr', test);
console.log("COOKIE", $.cookie("usr"), Ember.$.cookie('usr'), "cookie.length=", document.cookie.length);
Which results to the output "COOKIE undefined undefined cookie.length=0".
I don't see why I don't get my value out, seems like a simple thing. Any help is much appreciated.

I found the problem - needed to run the project from localhost to test.

Related

ngCookies doesnt work properly after i refresh the page

Im using angularjs in my project , and when the client log in , than a service save his information in the cookies .
by using this code:
$cookies.put('username',$rootScope.username);
or
$cookies.username = $rootScope.username;
i try them both.
in the first try , i tried : console.log($cookies.get('username'); and it doesnt work at all.
but in the second try when i tried : console.log($cookies.username); it did print the information.
but after i refreshed the page , this $cookies.username; didnt work anymore..
what maybe the problem?
To access the cookie you need to do $cookies.get('username')
It seems in your first console.log there is a typo, you're missing the last parenthesis
Try this
console.log($cookies.get('username'));
Also, make sure your $rootScope.username attribute has a value in it

Get jquery cookie using angular

I'm using jquery.cookie v1.4.1 to set a cookie like this
$.cookie('userCookie', $("#user").val());
Where $("#user").val() is returning something like 'username'
Then in an angular app, am trying to retrieve this cookie using
var userCookie = $cookies.get('userCookie');
But is not working, I'm getting:
var userCookie = undefined
I'm using AngularJS v1.4.8 with corresponding ngCookies
Any help would be nice....
Thanks to charlietfl I got it to work.
It happens that I was setting the cookie for a specific path, let say '/domain/somepath' and then I was trying to retrieve it from '/domain/someotherpath', where the cookie was not available.
I fix it specifying the path on the cookie to all my domain like this:
$.cookie('userCookie',$("#user").val(), { path: '/' });
Why don't you use angular to store your cookies?
$cookies.put('userCookie', $("#user").val());
Btw: its horrible if you use JQuery like this inside your Angular App.
$cookies.get('userCookie');
Your problem maybe: the cookie will be created BEFORE your DOM-Element is even rendered on the page. Try $.cookie('userCookie', 'test123'); , this should work pretty sure (with angular 100%).
How to fix this? First of all, DONT USE JQUERY LIKE THIS IF YOU HAVE ANGULAR!!!
<input ng-model="username"></input>
$cookies.put('userCookie', $scope.username);

Simple MEAN Stack issue

I have a express route;
router.get('/uni/:queryst', function (req, res) {
var choice = req.params.queryst);}
Where basically I use the queryst value from the URL to use it to query something in my database. On the front end, I need to have an input field where the user types the choice and hits submit and then it translates to something like /uni/valuehere on the browser and sending GET to express where it gets caught by the router I wrote above. I don't know how can I make the input field of HTML turn into a URL of the format /uni/valuehere .
As for testing the code initially I was directly typing the URL manually with the value I intended to verify to check if the data passed. Well, users are not going to type URL's directly to search, they will need a form and that is what I can't get my head around to how to turn the input field into a URL in a format /uni/WhateverTheUserHasTypedInTheFieldShouldAppearHere
Thanks in advance for the help. I am self learning the MEAN stack and have come a long way. I need support of experienced developers as I am currently stuck here.
While sending the request write (in controller):
$http.get('/uni/'+queryst).success(function(response){
// Write code using response
});
While on the server side write:
$http.get('/uni/:queryst',function(req,res){
var choice= req.params.queryst
// Code ....
});

Script error: "Unable to get value of the property 'split': Object is null or undefined

I searched around, and couldn't find an answer to my question. I'm very new at coding, and at work, we have an application that current names that are logged in, and what they are doing.
Recently, they have changed from jquery 1.4.1 to jquery 1.8.3. Ever since then, I cannot get the results to process correctly, because of the following error;
"Unable to get value of the property 'split': Object is null or undefined"
I have the code setup to grab the results and split them;
function processAgents(xData, status) {
var avail = xData.responseText.split("|")[0];
var acw = xData.responseText.split("|")[1];
var total = xData.responseText.split("|")[2];
var breaks = xData.responseText.split("|")[3];
var pending = xData.responseText.split("|")[4];
The application is setup to open as an HTA file which opens up the PHP script.
Any help would be appreciated, please let me know if I left anything out!
Thanks!
EDIT 1
I did some more investigating, and it looks like I'm not getting data from my process request. This is how it is currently setup
function updateAgents() {
var ts1 = new Date().getTime();
$.ajax({
url: "http://SERVER/AgentSrc.php?x=" + ts1,
complete: processAgents
I'm not sure if this is processing correctly since they went to jquery 1.8.3.
EDIT 2
So after looking into it more, it doesn't appear that the script is getting the data from the server, even though I have access. If I make a local file and put the information in it, it will pull the information and split it, but if I point to the path of the file on the server, it won't get the information. But the strange thing is, if I run it using jquery 1.4.1, it pulls the data fine, but can't display it. But with 1.8.3, it doesn't allow me to pull it from the server.
thanks again!
This will give some clarity
xData.responseText.toString().split("|")[0];
(split is part of string not jQuery)
Here is a possible explanation: in earlier versions of jQuery, ajax calls returned an xmlHttpRequest (XHR) object. Recent versions return a promise (jqXHR) instead.
See this page for more details.

connecting javascript to a Web API

I am new to the web development world and I would like to be able to connect an HTML page to a web api through . and I was really not successful in this.
I followed this tutorial to be able to make this connection : http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
All I need is to send some inputs from an HTML page to a web api that takes these parameters and returns an object
I am using this code
$.getJSON("api/GeneratorController/setparameters/"+firstparameter+"/"+secondparameter+"/"+thirdparameter+"/"+fourthparameter+"/"+fifthparameter+"/"+sixthparameter,
function (data) {
alert(data); //never comes here
}).fail(function (jqXHR, textStatus, err) {
alert("All checks are correct, image was not generated. jqXHR = " + jqXHR.valueOf() + " textStatus=" + textStatus + " Error" + err);
});
it always goes into the fail portion , I attached the alert message that comes out of it
Any Reason why it is doing this ?
#smartmeta (I changed the typo , thanks) I followed your advice and here is the output of the alert (as expected , values that I have inserted are displayed):
Your url needs to start with your domain, not 'api/generatorcontroller/...'. If you are developing locally, something like http://localhost:[port]/api/generatorController/....
Also, webApi maps to url verbs, (get, post, put, delete..), not functions like setparameters, unless you have a [name=setparameters] above your get() function.
Also, I am pretty sure you don't have a route setup to handle the url with all those parameters. What you want to look at, as it seems your using jQuery, is jQuery.get documentation. The second example near the bottom shows where to place parameters. WebAPI will check for them in the body if they are not in the query string. so it would end up looking like:
$.getJSON("http://"+window.location.host+"/api/GeneratorController/setparameters", {parameter1: parameter1, parameter2:parameter2 ...});
Well, the first thing to check is to make sure that your server-side function is returning the values you expect. You can do this with Chrome's developer tools or with the Firebug Firefox extension, and I think IE10 has something equivalent, too. Go to the "net" tab, find the request corresponding to your API call, and take a look at what the server responded with.
Please add the line
alert("api/GeneratorController/setparameters/"+firstparemeter+"/"+secondparameter+"/"+thirdparameter+"/"+fourthparameter+"/"+fifthparameter+"/"+sixthparameter)
Then call your script and take the output of the alert into a browser. Then check if your application Handels that route.
By the way I think you have a typo. I guess it should be firstparameter.
I assume you would like to do
"api/GeneratorController?foo=Bar
But when you are new to this, I would suggest that you first try the example like it is. And After that you can start changing setails.
So I found what was the problem with my code
Two things :
1- I shouldn't use the word "Controller" when I call my API ,it should be api/Generator/...
2- the function name needs to start with "get" and not "set" since it "gets" the return value from the api
Thanks everyone!

Categories