Ajax is undefined - ajax.net

This is my code. When I run this I get an error: "Ajax is undefined". The error line is highlighted by black letters.
function funGetName()
{
var id=document.getElementById("Cust_Id").value;
var URL="Default.aspx?TODO=getName&custId="+id;
alert(id);
var ajax=new Ajax.Request(URL,
{
method:'get'
});
}

Try putting the opening brace on the same line, after URL,. It may be a semicolon insertion problem.
i.e.:
var ajax=new Ajax.Request(URL, {
method:'get'
});

Related

getting marker-symbol with $.ajax from *.geojson file

I'm using markers with data from a *.geojson file. It works with
var Symbol = L.icon({iconUrl: 'sonne.png'});
...
function onEachFeature(feature, layer) {layer.bindPopup(feature.properties.popupContent);}
$.ajax(myGeojsonFile).done(function(data) {
data = JSON.parse(data);
// Symbol = L.icon ({iconUrl: feature.properties.marker-symbol});
L.geoJson(data,
{pointToLayer: function (feature, latlng) {return L.marker(latlng, {icon: Symbol});},
onEachFeature: onEachFeature}).addTo(map);
});
But I want to set the marker-symbol in the *.geojson file. I altered the *.geojson file with a geojson editor, therefoere it schould be correct.
But it does not work, if I try to set the marker symbol with the "//-line" in the code above. How to fix this?
Thanks for thinking about the problem!
Gruss, wonk
it works with this:
$.ajax(overlay).done(function(data) {
data = JSON.parse(data);
L.geoJson(data,
{pointToLayer: function (feature, latlng) {return L.marker(latlng, {icon: L.icon({iconUrl: feature.properties.markerSymbol})});},
onEachFeature: onEachFeature}).addTo(map);
});
The property "marker-symbol" in the *.geojson file must not have a "-"character!
Gruss, wonk

Parse Cloud Code Save Issue

I wrote some backend code for a Parse.com mobile app a couple of years ago, and have just been asked to add a feature. However, I found that after a small tweak the code wouldn't succeed. So, I rolled back to the working copy, downloaded, then deployed that back and it wouldn't work either! I wonder if this is a change in the Parse software?
The code is failing at the save method as all the logs are fine until then. The log for the error case shows 'No message provided'. If I don't use the message attribute it just shows '{}', so I presume it's empty. I have put the promise resolution in the error case to stop the job timing out while I debug. One thing I have never understood is why I have to make two Seed objects and piggy-back off one to save correctly. If I did a.save(null,...) it wouldn't work.
Any help would be fantastic. Thanks!
PS: Apologies for the indenting below - it is correct in my file.
function flush() {
//Clear the previous records from the class.
var Seed = Parse.Object.extend("Seeds");
var _ = require("underscore");
var arr = [];
var query = new Parse.Query(Seed);
return query.find().then(function(oldSeeds) {
_.each(oldSeeds, function(oldSeed) {
arr.push(oldSeed.destroy());
});
return Parse.Promise.when(arr);
});
}
Parse.Cloud.job("fetchjson", function(request, status) {
var url = 'someurl';
flush().then(function() { Parse.Cloud.httpRequest({url: url}).then(function(httpResponse){
var Seed = Parse.Object.extend("Seeds");
var jsonobj = JSON.parse(httpResponse.text);
var _ = require("underscore");
var results = [];
// do NOT iterate arrays with `for... in loops`
_.each(jsonobj.seeds, function(s) {
var p = new Parse.Promise();
results.push(p); // Needs to be done here or when() will execute immediately with no promises.
var seed = new Seed();
var a = new Seed(s);
var image_url = a.get("image")
//Get the JSON.
Parse.Cloud.httpRequest({url: image_url}).then(function(response) {
console.log("Fetching image at URL: " + image_url);
//Create a new image object and save, passing ref through promise.
var file = new Parse.File('thumb.jpg', { base64: response.buffer.toString('base64', 0, response.buffer.length) });
return file.save();
}).then(function(thumb) {
console.log("Attaching thumb to object");
//Set image ref as object attribute.
a.set("imageFile", thumb);
console.log("Parsing views into viewsint");
//Save decimal string as int into another attribute.
a.set("viewsInt", parseInt(a.get("views")));
console.log("Parsing description into descriptionarray");
//Save string as array into another attribute.
var dar = new Array(1);
//dar[0] = a.get("description")
a.set("descriptionarray", [a.get("description")]);
}, function(error) {
console.log("Error occurred :(");
}).then(function(){
console.log("Saving object");
//Save the object and resolve the promise so we can stop.
seed.save(a,{
success: function(successData){
console.log(successData);
p.resolve(successData);
},
error: function(error){
console.log(error.message);
p.resolve(error);
}
});
});
});
// .when waits for all promises to be resolved. This is async baby!
Parse.Promise.when(results).then(function(data){
console.log("All objects saved");
status.success("Updated Succesfully");
});
}, function(error) {
//Oh noes :'(
console.error('Request failed with response code ' + httpResponse.status);
status.error("Update Failed");
});
});
});
I changed your code a bit and put some comments to explain:
// DEFINE THESE ON THE TOP. NO NEED TO REPEAT.
var _ = require("underscore");
var Seed = Parse.Object.extend("Seeds");
function flush() {
//Clear the previous records from the class.
var arr = [];
var query = new Parse.Query(Seed);
return query.find().then(function(oldSeeds) {
_.each(oldSeeds, function(oldSeed) {
arr.push(oldSeed.destroy());
});
return Parse.Promise.when(arr);
});
}
Parse.Cloud.job("fetchjson", function(request, status) {
var url = 'someurl';
flush().then(function() {
Parse.Cloud.httpRequest({url: url}).then(function(httpResponse){
var jsonobj = JSON.parse(httpResponse.text);
var results = [];
_.each(jsonobj.seeds, function(s) {
// ONE SEED OBJECT WITH INITIAL SET OF DATA FROM JSON
var seed = new Seed(s);
var image_url = seed.get("image")
// A SERIAL PROMISE FOR EACH SEED
var promise = Parse.Cloud.httpRequest({url: image_url}).then(function(response) {
console.log("Fetching image at URL: " + image_url);
//Create a new image object and save, passing ref through promise.
var file = new Parse.File('thumb.jpg', { base64: response.buffer.toString('base64', 0, response.buffer.length) });
return file.save();
}).then(function(thumb) {
// SETTING MORE PROPERTIES
//Set image ref as object attribute.
console.log("Attaching thumb to object");
seed.set("imageFile", thumb);
//Save decimal string as int into another attribute.
console.log("Parsing views into viewsint");
seed.set("viewsInt", parseInt(seed.get("views")));
//Save string as array into another attribute.
console.log("Parsing description into descriptionarray");
seed.set("descriptionarray", [seed.get("description")]);
// SAVING THE OBJECT
console.log("Saving object");
return seed.save();
});
// PUSH THIS PROMISE TO THE ARRAY TO PERFORM IN PARALLEL
results.push(promise);
});
Parse.Promise.when(results).then(function(data){
console.log("All objects saved");
status.success("Updated Succesfully");
});
}, function(error) {
console.error('Request failed with response code ' + httpResponse.status);
status.error("Update Failed");
});
});
});
Thanks knshn. I had refactored the code a lot since that version (including several of the changes you made), but I had posted the version that was identical to that which was working fine before. Your changes let me see the right error. For some reason doing the simple single object implementation didn't work for me originally, hence the nasty workaround. It works now though.
I have now found the culprit - the Seed class had an attribute called 'id'. With the old version this worked fine, but when I deployed that code now it gave an error 101: 'object not found for update'. This must be because the new Parse code is mixing that up with the internal objectId and getting confused that the id is different to what it expects. I wonder how that could still work with the rollback though. Perhaps the at version was tagged to use the older Parse code.
My fix was to use a different name for the id - 'seed_id'.

Parse Query equalTo not Working

I'm trying to simply query my database filtering results with a parameter (named: "lesson") I've just obtained from another query.
The problem is that when I try to print with an "alert" command lesson's value it says: undefined.
It is wierd that when I put "lesson" inside a tuple's field it works.
In particular line
obj.set("student", lesson);
added just for debugging purpose, actually writes a consistent value into the table.
This is the whole code:
Parse.Cloud.job("checkingTwoTables", function(request, response) {
Parse.Cloud.useMasterKey();
var stud,lesson;
//select first student of the list and check for his enters
var wholeTable = new Parse.Query("enter");
wholeTable.find({
success: function(result) {
if(result.length != 0)
{
//pick student name and lesson we're interested in
stud = result[0].get("student");
lesson = result[0].get("lesson");
}
else {}
},
error: function(error) {
console.log("failed");
}
});
alert("lesson value:" + lesson);
var selectionQuery = new Parse.Query("enter");
selectionQuery.equalTo("lesson", "cns");
selectionQuery.find({
success: function(results) {
for (var i = 0; i < results.length; i++)
{
var obj = results[i];
obj.set("raggiunto", 77);
obj.set("student", lesson); // <<-------HERE IS THE ISSUE
obj.save(null,{
success: function (object) {
response.success(object);
},
error: function (object, error) {
response.error(error);
}
});
}
},
error: function(error) {
console.log("failed");
}
}); //code continues...
You are having this exact issue.
.find() is making a request with the server. You cannot guarantee when it would finish thus you cannot guarantee that your second query will run after the first one completes. Therefore, when your second query runs, the value of lesson may still have not been retrieved yet by the first query, hence the "undefined".
The solution would be to move the second query inside the success block of the first query.

SinonJS 101 error

I'm try to learn this tech and somehow getting stuck at the opening.
Please tell me why this test isn't working. What obvious thing did I miss?
var myfunc = function() {
alert('hello');
}
test("should spy on myfunc", function() {
var mySpy = sinon.spy(myfunc);
myfunc();
sinon.assert.calledOnce(mySpy);
});
It's the scope of myfunc. This works:
var o = {
myfunc: function() {
alert('hello');
}
};
test("should spy on myfunc", function() {
var mySpy = sinon.spy(o, "myfunc");
o.myfunc();
sinon.assert.calledOnce(mySpy);
ok(true);
});
The reason your test is not working is because you're not invoking the spy, rather the original function.
And the reason #carbontax's example works is because in that case, o.myfunc is replaced by the spy automatically; so when you invoke o.myfunc, you're actually invoking the spy.
As Mrchief said, you are not invoking spy but calling myfunc();, you should invoke spy something like.
test("should spy on myfunc", function() {
var mySpy = sinon.spy(myfunc);
mySpy(); // <= should called instead of myfunc()
sinon.assert.calledOnce(mySpy);
});

CasperJS code from tutorial does not work

It's my first post here : )
I'm learning CasperJS and I have to write script who search all img's on site and check urls.
I found this tutorial from vgaltes.com
var imagesArray = [];
function getImages() {
var scripts = document.querySelectorAll('img[src]');
return Array.prototype.map.call(scripts, function (e) {
return e.getAttribute('src');
});
};
casper.start('http://fooo.fooo', function () {
imagesArray = this.evaluate(getImages);
var self = this;
imagesArray.forEach(function (item) {
if (self.resourceExists(item)) {
self.echo(item + ' loaded');
} else {
var message = item + ' not loaded';
self.echo(message, 'ERROR');
}
});
});
but when I run this code on CasperJS (with valid url) do not work. Nothing happens.
Casper Version is 1.1
Looks like you did not run the function, try adding the below code in the end
casper.run(function() {this.test.renderResults(true);});
I'm the owner of vgaltes.com. As Pbk1303 said, you have to call to run function. If you read the tutorial, is the last source code posted.
casper.run(function(){
this.echo('finished');
this.test.done(1);
this.test.renderResults(true);
});
Regards,

Resources