Error : expecting ] token after element when using ajax.get - javascript

I keep getting an error when i run the debugger that says "SyntaxError: missing ] after element list"
the first request always works as intended but all of the follow requests return a syntax error. can someone explain to me what i'm messing up here. please and thankyou.
$("#submitmsg").click(function(){
alert('start');
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
return false;
});
setInterval($.ajax({
url : 'http:www.whatever.com/log.html',
type : 'GET',
success : function(data)
{
document.getElementById('chatframe').innerHTML = data;
}
}), 500);
</script>

your setInterval is wrong, it should be, setInterval(func, delay[, param1, param2, ...]); like
setInterval(ajax_func, 500);
function ajax_func() {
$.ajax({
url : 'http://www.whatever.com/log.html',
type : 'GET',
success : function(data) {
document.getElementById('chatframe').innerHTML = data;
}
});
}

Related

Sending a variable from JavaScript to Java

I've created a game that collects a User's time on the browser with JavaScript. What I'd like to do is grab that JavaScript data and send it to the backend, which is written in Java (I'm using a local server running on Tomcat). How can I accomplish this?
I've looked into AJAX and here is what I've come up with...
var myTime = // however long it took for user to win game
var data = {}
data["myTime"] = myTime;
$.ajax({
type : "POST",
url : "/path-to/hosting/save",
data : JSON.stringify(data),
dataType : 'json',
timeout : 100000,
contentType:'application/json',
success : function(data) {
console.log("SUCCESS: ", data);
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
console.log("DONE");
}
});
When I finish the game, I receive this error on the console:
statusText:"parsererror"
My initial thought was that I didn't form my JSON correctly, but I am not sure. Any help would be appreciated. Thanks in advance!
As your code it seems like myTime is a single value why you store it in array you can pass that as follows
var myTime = // however long it took for user to win game
//var data = {}
//data["myTime"] = myTime;
$.ajax({
type : "POST",
url : "/path-to/hosting/save",
data : JSON.stringify({
'myTime': myTime
}),
dataType : 'json',
timeout : 100000,
contentType:'application/json',
success : function(data) {
console.log("SUCCESS: ", data);
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
console.log("DONE");
}
});

Parse Cloud result object is empty

I got this problem yesterday, couldn't resolved it myself.
Here is my parse.com cloud code :
Parse.Cloud.define("getWorkerInfo", function(request, response) {
Parse.Cloud.useMasterKey();
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo(request.params.userObjectId);
userQuery.select("firstName", "lastName", "username");
userQuery.first({
success : function(result){
var currentUser = {
"firstName" : result.firstName,
"lastName" : result.lastName,
"username" : result.username
};
response.success(currentUser);
},
error : function(error){
response.error(error);
}
});
});
and result to chrome dev tools command :
result : {}
this code for using result from first code wrote this question :
function get(userObjectId) {
Parse.Cloud.run("getWorkerInfo", { "userObjectId" : userObjectId }, {
success: function(result){
return result;
}
,
error : function(err){
console.log(err);
}
});
}
I also changed to response data - user query directly.
Response action is okay.
But Couldn't retrieved that itself :
result = ParseUser {_objCount: 2, className: "_User", id: "iNneJil9XW"}
What I missing, and can I find answer this problem?
Thanks Clark, there's a lot going on in this question. Try hard coding the result just to make sure the execution flow/path is correct and the environment is set up correctly. For example,
Parse.Cloud.define("getWorkerInfo", function(request, response) {
Parse.Cloud.useMasterKey();
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo(request.params.userObjectId);
userQuery.select("firstName", "lastName", "username");
userQuery.first({
success : function(result){
response.success("1234");
},
error : function(error){
response.error(error);
}
});
});

Uncaught TypeError: Cannot read property 'token' of undefined

My complete code is here http://pastebin.com/xwJ2zi2y. I am trying to do, when the value of Ajax refresh_token function value change it should call the order_list function. when I refresh the page, it gives the output of order_list function once but when It start interval the refresh_token it gives the error "Uncaught TypeError: Cannot read property 'token' of undefine" on line 94 in pastebin code. And did not run order_list function, when the value of refresh_token changes.
Thanks in advance.
You need to store the context for the usage in the success callback. Here your fixed function:
refresh_token : function(){
var self = this;
$.ajax({
type: 'GET',
url: "/order/refresh",
headers: {
Accept : "application/json",
"Content-Type": "application/json"
},
success: function (resp){
var newToken = resp;
console.log(newToken); //it give the value of refresh eg. ["20150925313"]
if(newToken != self.state.token){
self.setState({ token: newToken});
// console.log(this.state.resp);
}
}
});
},
shouldComponentUpdate help me to resolve this issue. see the below code before render function.
shouldComponentUpdate: function(nextProps, nextState) { return true;
},
my complete running code is here http://pastebin.com/xwJ2zi2y.
Thanks

Fetch http status code in jquery

Below is an existing jquery code in our code base.
$("#download_").click( function() {
$("#error").html('');
$.ajax({
type : "GET",
cache : false,
async : false,
url : "/download",
success : function(data) {
var json_obj = $.parseJSON(data);
if(json_obj !== undefined && json_obj != null){
if(json_obj.download=="success"){
location=json_obj.url;
}
}
},
error : function(data) {
// TODO
$("#error").html(failed);
}
});
});
Here, In case of error (marked as TODO), I want to check if the http status is 404, then I need to redirect user to different url.
Can any one tell me how do I get the http status in this error: function(data) method?
Thanks!
Did you even look at the docs?
$.ajax({
...
statusCode: {
404: function() {
alert('page not found');
}
}
});
try: statusCode
from documentation:
$.ajax({
statusCode: {
404: function() {
alert('page not found');
}
}
});
http://api.jquery.com/jQuery.ajax/
EDIT:
Now that I think about it, do you only want to redirect if it's a 404? What about other error codes?

MooTools: How to use responseText directly

In following example of code, I want to traverse the responseText object which consist the html code came from request_page.php file. In onSuccess event, i want to check whether < Div > with id 'ersDiv' has any errors posted in it.
new Request.HTML({
url: 'request_page.php',
onSuccess: function(responseText, responseXML) {
// My expected code to handle responseText object
alert(errorMessage);
},
onFailure: function() { }
});
request_page.php file is like this :
<div align='center'><div id='ersDiv'>Page loaded with insufficient data</div></div>
try this for 1.2.x (example tweaked for the jsfiddle api):
new Request.HTML({
url: '/echo/html/',
method: "post",
data: {
html: "<div align='center'><div id='ersDiv'>Page loaded with insufficient data</div></div>",
delay: 1
},
onComplete: function(responseText, responseXML) {
var error, errors = responseXML.getElements("div").filter(function(el) {
return el.get("id") == "ersDiv";
});
if (errors.length) {
error = errors[0].get("text");
alert(error);
}
}
}).send();
working example:
http://www.jsfiddle.net/dimitar/vLgqB/
in 1.3 this can work as oskar suggested:
console.log($$(this.response.tree).getElement("#ersDiv")[0].get("text"));
http://www.jsfiddle.net/dimitar/vLgqB/2/
have fun.
function(responseText, responseXML) {
if (responseText.getElement('#ersDiv').get('text') === 'Page loaded with insufficient data'){
console.log('There was an error');
}
}
Btw. 1990 called, they want their align='center''s back :-)

Categories

Resources