I am generating a json object from php with json_encode and consume it with JS. The JSON object is valid, i run it through JSON lint, but I am always getting a parseerror when it is consumed from the php file directly.
Now, if i generate a .json file and output the object there no error is raised.
Has anyone come across this problem? Any thoughts as to why this might be happening?
Thank you. Any help is greatly appreciated.
Try not passing the JSON object to the JS file but instead echo it out on the php file then tell js in an output function of some sort to pick it up1
eg
echo json_encode // In php file
function(output){
('some element').html(output); //output is what is returned from the php file (the json)
}
Related
I need to store javascript data into a ulg (Ulog) file. In other words, I need to create a ulg file and convert JavaScript values into ulg and then store it. Can anyone give me some ideas about this?
I had a look into PX4 but it does not support this and I haven't found any post about this either.
FYI: It is also valid the conversion of another type of file other than JavaScript as long as I can convert JavaScript into that type of file. For example: .js -> .ulg or .js -> .py -> .ulg
The end result needs to be an ulg file
I really appreciate the help!
Have a look at this link: https://github.com/PX4/pyulog
Basically, you have to call the ulog2csv script.
For example: in your log’s folder, open a terminal and enter ulog2csv log_name.ulg.
I have a D3 graph which reads a JSON file. Where I am struggling is, what is the mechanism to get the dynamically generated PHP JSON data into the D3 graph.
I think the obvious solution from someone like myself with limited knowledge would be to have PHP write a JSON file to a directory on load, and then have the D3 graph look for it.
However, I feel like there is a better way to do it without writing a json file and reading a json file.
Could anyone please suggest the industry standard method for something like this, so I can continue my reading.
Just request the PHP script from D3 like it is a JSON file. Then in your PHP script...
$data = array('your', 'data', 'here');
header('Content-Type: application/json');
echo json_encode($data);
exit();
I am trying to display the raw contents of a .php file using javascript but i am unable to get it. The php file is in my localhost.
PHP is server code. JS is client code. (Execution talking)
You can't access your PHP files from your .js
The only thing you can do is to get some PHP output using Ajax but I think that is not what you are aiming for.
You can use show_source() function to output the file with the PHP syntax:
<html>
<body>
<?php
show_source("test.php");
?>
</body>
</html>
This function has to parameter show_source(filename,return). filename is required and second parameter is optional if this parameter is set to TRUE, this function will return the highlighted code as a string, instead of printing it out. Default is FALSE
There is no way to use client side code to cause the server to provide the server side source code.
You would need to work on the server to create a URL which provided the data you want.
You could then fetch it using a link, JavaScript's XMLHttpRequest or whatever other method took your fancy.
I'm just learning php, and I am trying to parse some files I have, and then send the information to javascript. This is my php:
$datesArray = readFile();
echo json_encode($datesArray);
This echoes:
["120102000000","120102000500","120102001000","120102001500","120102002000","120102002500","120102003000"]
Which is what I want. However, my javascript, which looks like this:
var dates = <?php echo json_encode($datesArray); ?>;
console.log(dates);
keeps giving dates back as null, and I am not sure why. I have tried encoding the array as utf-8, and I have tried using jQuery.parseJSON which also did not work.
Thanks--
Just checking, but is that JavaScript with PHP in a .js file? By default, Apache only runs PHP in files ending in .php so that would mean your PHP is getting parsed as plain text by the server.
If that is the case, the easiest solution is to run that part of the script in some <script> tags.
Alternatively you could make Apache try to serve .js files as PHP file via Mime-Types; but that is really a mess not worth implementing.
I'm loading a large number of items on a page through JSON, but it isn't working. This is my first time using JSON and I figured JSON was just a big object so I copied my object that I had before in a variable into a file and names it fun.js.
You can check out the JSON here:
http://justpaste.it/15zc
I'm using jQuery to get the JSON:
$.getJSON('fun.js', function(data){
alert(data)
});
Nothing is being alert, in the matter of fact...the alert isn't happening at all. Anyone know why?
For starters, your JSON doesnt validate. Go paste it here and fix your errors: http://jsonformatter.curiousconcept.com/
Secondly, user jQuery.Ajax to which you can pass onError parameter so you'll get a warning that JSON didn't go through.
Have you validated that the JSON is the issue? Open up Firebug or Chrome Dev Tools and refresh the page. You may see a message that the file wasn't found or there was a parsing exception or a security error.
If the file isn't found, you can fix that in the code.
If there is a parsing error, use a JSON validator.
If there is a security issue, see my answer to Get a JSON file from URL and display. You need to update your server policy or configure your browser to allow access to file URL's in your tests through.