Why is alert in microAjax not outputting text echoed from php file? - javascript

I am trying to get a string from a text file. I have a php file called genjsonGPS.php that looks like this:
<?php
$myFile = "vesselGPS.txt";
$fh = fopen($myFile, 'r');
$vesselGPS = fread($fh, filesize($myFile));
fclose($fh);
echo("Hello");
?>
When I run it, I get for an output in my browser:
"Hello"
I have a webpage with a js file using microAjax (from the js file):
microAjax("genjsonGPS.php", function (res) {
alert (res);
});
When I run the webpage I get a blank alert. When I change the function above to:
microAjax("genjsonGPS.php", function (res) {
alert (res.toString);
});
I get an alert that says:
function toString() { [native code] }
How do I get the function above to output "Hello" in the alert like the browser does?

Please follow these tips.
Monitor your HTTP server's access log, something like tail -f /var/log/apache/access.log is helpful. (Assume you are working on *nix platform)
If you are using Chrome, please check out the network monitor panel.
You'd better make sure that:
Your php page is truly accessed.
There is no error occurs, both in server and client sides.
If you still cannot figure out where things goes wrong, if would be useful to let people know how microAjax is implemented.

What happens if you put the URL like "/genjsonGPS.php"?

Related

Mail Rules with javascript instead of applescript

In a Mac mail rule, I am trying to run a javascript instead of an applescript. This has been asked in Mail Rules using JavaScript for Automation instead of AppleScript but the answer is not working for me!
I have tried to simplify the code as much as I can. So, the following applescript works fine:
on perform mail action with messages theMessages
say "running!"
end perform mail action with messages
but the equivalent javascript is not working.
function performMailActionWithMessages(messages) {
app = Application.currentApplication()
app.includeStandardAdditions = true
app.say ("running")
}
Edit
Here are my rule parameters
I do it without getting app. Try something like…
// ObjC.import('Cocoa')
ObjC.import('stdlib')
var program="/usr/bin/say"
function performMailActionWithMessages(messages) {
say("running")
}
function say(what){
var command = program + " '"+what+"'"
console.log(command)
$.system(command)
}
I’m not sure you need cocoa. You probably need stdlib.

Swift JavaScriptCore not working with large files?

https://github.com/gg2001/monero/blob/master/monero/NewWallet.js
I have a js file that is quite large 6000 lines and JavaScript core does not seem to be able to retrieve variable values whereas running the same file in any web browser works fine for me. When I try to retrieve the value of a variable it shows up as undefined, but when I use a js console in a browswer it shows up fine. I am speculating that this is due to the size of the file because when I put
var helloWorld = "Hello World";
in the front of the js file this swift code can retrieve it
func helloWorld() {
if let variableHelloWorld = self.jsContext.objectForKeyedSubscript("helloWorld") {
print(variableHelloWorld.toString())
}
}
but when I put it at the end it cannot.
Normally this indicates a parsing error. Try adding an error handler to self.jsContext before calling objectForKeyedSubscript() and see if it outputs anything insightful.
self.jsContext.exceptionHandler = { context, exception in
print("JS Error: \(exception?.description ?? "unknown error")")
}
Although your JS code may be valid in a browser console, iOS Safari doesn't support as many Javascript features as newer browsers.
I did see a line in your JS source code beginning with just a semicolon (followed immediately by (function). I wonder if the parser might complain about an empty line without a statement..? Maybe nothing, though.

Setting variable from .CTP view to .JS

I'm having an extremely annoying and non-sense problem, are you prepared? :x
Ok, let's go:
Actualy I have an specific software deployed into 4 environments : Development, Certification, Preproduction and Production.
Yesterday I've received an error report in Production environment.
Thing is when I try to set a javascript variable from my .ctp to my .js my .js cant access that variable so the 'set' is not done. The weird thing is that in other environments (DES, CERT and PRE) that works perfectly but in PRO (for a certain casuistic it fails).
Sketching the problem:
translate.ctp
<?php
$translations = array();
$translations['NO_DATA']=__('NODATA');
$translations['VALUE']=__('VAL');
$translations['WEBROOT'] = $this->webroot;
$this->Js->set('translations',$translations); // <-- prepare 'translations' into JS as window.app.translations
echo $this->Js->writeBuffer(array('onDomReady' => false)); // sets the variable 'translations' into the javascript (I only use writeBuffer once in this code and project)
?>
default.ctp
...headers, script loads and stuff...
<?php echo $this->element('translate');?> // loads translations ctp
<?php echo $this->fetch('script'); ?>
main.js
$(document).ready(function() {
// window.app doesn't exists so the following instruction will trigger an error:
var translatedNoData = window.app.translations['NO_DATA'];
}
Thank you very much guys, I hope the info above is enough.
I'm updating this post in order to solve the 'issue' posted above.
The code above is OK. $this->Js->set.. and $this->Js->writeBuffer are the correct way to set a variable from .CTP directly into the .JS as a global variable (it's gonna be set inside window.app)
Problem was caused by data inserted by the user. He putted some ' without closing it in a certain column from DB table which content was rendered directly into an HTML DOM element, so it crushed everything coming next...
Sry for the missunderstanding :)

How to print front-end log messages on the server console?

Is there a way to print log messages to the server console from front-end?
For Example, if my GUI HTML pages is running on JBOSS server. I want to write logs to the server console ie, "SERVER.log" file generated on the JBOSS server.
(Just like Sysout() for printing simple debugging messages.)
You could overwrite the default console.log api or use another method;
var logHandler = {
send: function(args) {
$.post("URL", {
messages: arguments
});
}
}
window.defaultConsoleLog = console.log;
console.log = function() {
logHandler.send(arguments)
defaultConsoleLog.call(this, arguments);
}
Not tested code
I would recommend not overwriting default behavior of the browser, but instead have a "proxy"
var logger = function() {
logHandler.send(arguments);
console.log.call(this, arguments)
}
And then use logger("Test") instead of console.log("Test")
Can you be more clear on the "server console" part and your specific requirements?
If all you need is to capture activity on the server, I suggest you look into something like log4javascript.
http://log4javascript.org/docs/index.html

NS_ERROR_FAILURE with JS XMLHttpRequest

I'm trying to load the source of a web page, as shown in "Javascript:The Definitive Guide" p.481, using firefox.
Here's my code:
var request = new XMLHttpRequest();
request.open("GET", "http://finance.yahoo.com/q?s=PG", false);
request.send(null);
if (request.status==200) { alert(request.responseText); }
else {alert("Error "+request.status + ": "+request.statusText);}
</script>
Firebug shows the GET statement, followed by 200 OK X 338ms.
Which looks like it was successful (code 200).
But the next Firebug line says: NS_ERROR_FAILURE, request.send(null);, with no further explanation.
and neither alert gets executed.
It doesn't help if I use help mode, and doesn't run in Chrome either. I don't have a popup or cookie or ad blocker running.
The page loads fine in perl with an LWP get().
If this is an inevitable cross-domain taboo, why doesn't the "definitive guide" say so?
I understand that JSONP returns JSON. I don't want that - I want just a string with the raw source, like with the perl LWP get().
Is this impossible with Javascript?
I suppose I could write a batch file which uses perl to get the source and put it into a JSON file whose name is hard-coded into the JS. But I'd like to avoid that sort of kludge.
In case it helps,
I have run into this problem a couple times before, and ran into it again. If the answer to this question doesn't help you, try adding the following lines which helped me:
request.overrideMimeType('text/xml; charset=UTF-8'); // needed to get utf8 req's to work
or this debug line (which you can monitor on the web console of your browser)
request.onreadystatechange = function() {console.log("statechanged. url= " + url);};
before your
request.open()
where url is a var that holds the url you are trying to request. Sometimes it will give you that cryptic error if there is a problem getting the url, in my (last) case, it was because the url was malformed. Hope that helps someone out there.

Categories

Resources