Does JSONP impose some size limit on the response? - javascript

I have implemented a simple PHP script to provide JSONP support for a set of JSON files I want to be accessible via cross-domain requests.
Here it is:
<?php
$jsonFile = $_GET['resource'] . ".json";
$fh = fopen($jsonFile, 'r');
$jsonData = fread($fh, filesize($jsonFile));
fclose($fh);
$jsonData = trim($jsonData);
header("Content-type: application/json");
echo $_GET['callback'] . '(' . $jsonData . ');';
?>
This works great when I type the url in manually.
If my URL is something like:
http://mywebserverdotcom/jsonp/data.php?resource=jsondata&callback=processJsonData
I see the response in the form of:
processJsonData([{"record_id":"317", ...}]);
and my data is complete and everything looks good.
However, when I try this using the following method in my HTML/JS:
1) I added a <script> element at the bottom of my HTML file with the URL above
2) Implemented a JS file with the callback function
I get an error. I used Web Inspector to see the error, and it shows an error on the callback, and it looks like the callback is cut off at about 200 characters or so (I didn't count) characters into the response, so the response is now:
processJsonData([{"record_id":"317", ...
The data is cut off, so the JSON format is messed up and there is no closing ); at the end of the function call, which creates an error. The error is: processJsonData variable not found.
So... Either I'm just doing this all wrong, OR there is some size limit in the response size allowed using a JSONP callback via the script element, OR something else I'm not thinking of....
Any help much appreciated!
Thanks

No, nothing about using JSONP should be limiting the size of your response. As far as the HTTP transport layer is concerned you are just sending some text data from the server to the client; it doesn't really care what that text data is or how it is structured internally.
Probably the issue is somewhere in the server-side code. Can you post the PHP you are using?

Make sure your JSONP response script is included after your script containing the callback function. The error message seems to indicate you had the script tags out of order. Your script tags should be ordered like this:
<script type="text/javascript" src="myscript.js" />
<script type="text/javascript" src="jsonprequest.php?callback=processJsonData&arg=1" />
A script tag's JavaScript is not executed until all previous scripts have executed. When your JSONP request script executes, it expects the handler to already exist. But if your script containing the handler isn't included until after the JSONP script, that is too late.

Here's a sampling of the data. I've left in the callback at the front. This is just part of the data, so the ending ]); are not included. It's public data. I know it's valid JSON because I'm using the exact same file using Ajax instead of JSONP to load it from my local machine and it works fine. It's only when accessing via this JSONP/PHP script from a remote server that it fails with the error. The exact error message is:
ReferenceError: Can't find variable: callback
and the location of the error is data.php:1 which is my remote PHP script.
callback([{"record_id":"317","artist":"Vern Luce","title":"Untitled","date":"1983","medium":"Painted steel","discipline":"sculpture","dimensions":"a: 93 \" x 40 \" x 64 \", b: 76.5 \" x 31 \" x 29 \", c: 48.5 \" x 85 \" x 20 \"","funding_source":"CETA","location":"MacLeay Park","street":"NW 29th Ave and Upshur St","city":"Portland","state":"OR","zipcode":"","lat":"45.535999799999999","lng":"-122.7110045","description":"Three geometric abstract steel sculptures are placed in a raised landscaped area in and located directly south of the Thurman Street Bridge. In siting the work, the artist wanted the sculptures to respond both to the surrounding greenspace (thus, the bright red color) and to the broad horizontal expanse of the Thurman Street bridge (thus, the vertical nature of the sculptures). At the time the pieces were installed, Vern Luce lived near Lower MacLeay Park and selected the site both for its visual beauty and its proximity to his home.\n\nProject History\nThe Comprehensive Education Training Act of the early 70's provided grants to a number of Portland artists that enabled them to create artwork. As a result, over 500 works by 52 artists became part of the City of Portland's collection, providing a rich and diverse history of art in Portland. Aside from Lower MacLeay Park, two other Portland parks feature permanent sculptures acquired through this program: a sculpture by Bruce West in Lair Hill Park and a piece by Jerry Allen in Peninsula Park.","image_url":"http:\/\/data.racc.org\/pa_inventory\/0240\/0240thumb.jpg","detail_url":"http:\/\/racc.org\/public-art\/search\/?recid=317.192","date_modified":"2010-07-19 00:00:00"},{"record_id":"359","artist":"Bruce West","title":"BW1","date":"1978","medium":"Cor-ten steel","discipline":"sculpture","dimensions":"6' x 30' x 20'","funding_source":"CETA 1976-77","location":"Lair Hill Park","street":"3000 SW Barbur Blvd","city":"Portland","state":"OR","zipcode":"97201","lat":"45.501570100000002","lng":"-122.68130650000001","description":"","image_url":"http:\/\/data.racc.org\/pa_inventory\/0098\/0098thumb.jpg","detail_url":"http:\/\/racc.org\/public-art\/search\/?recid=359.185","date_modified":"2010-12-29 00:00:00"},{"record_id":"362","artist":"Jerry Allen","title":"Disc #4","date":"1979","medium":"Cast silicon bronze","discipline":"sculpture","dimensions":"diameter: 4 1\/2'","funding_source":"CETA 1977-78","location":"Peninsula Park","street":"6222 N. Albina Avenue","city":"Portland","state":"OR","zipcode":"97217","lat":"45.568221899999998","lng":"-122.6748716","description":"","image_url":"http:\/\/data.racc.org\/pa_inventory\/0102\/0102thumb.jpg","detail_url":"http:\/\/racc.org\/public-art\/search\/?recid=360.55","date_modified":"2010-03-12 00:00:00"},

Related

Simple online web game crash eventually

Background:
I am making a simple game in PHP, JavaScript and HTML for the web. A player control movements of a box on the screen, and see others fly around with their boxes.
I have the following files, that I upload to my domain via a hosting company:
index.html: a file with some buttons (eg. to start the game) and frames (for putting boxes in).
server.php: PHP script that receives messages from client, performs reads/writes to a database, echoes (using echo) boxes from database to the client. Does not echo the box of the player the message came from.
database.txt: a JSON text file containing data of players and the next free ID number. When empty it looks like this: {"players":[], "id": 1}. players contain objects with values such as ID, position and rotation.
script.js: JavaScript file with script to send/receive messages, display data from messages etc. Linked to index.html. Moves your box.
A screenshot, two players in movement:
Problem: The game crashes, always. Sooner or later. This is what happens:
Client recevies player data from server.php, everything is fine. This could be for 10 seconds or up to some minutes.
The data starts to falter, the message sometimes is null instead of actual data.
The data recevied is always null. The database file is now {"players":null,"id":5}. (The "id" could be any number, does not have to be 5).
Picture of data flow, printing of players from database. Two players. Before this screenshot lots of rows with valid data. Then as seen two null messages. Then after a while null forever.
I am not completely sure where the problem is, but I am guessing it has to do with my read/write in server.php. I feels like a lot of player movement makes the program more likely to crash. Also how often the program sends data affetcs.
Code Piece 1: This is code from server.php, that writes to the database. I have some sort of semaphore (the flock( ... ) ) to prevent clients from reading/writing at the same time (causing errors). I have an other function, read, which is very similar to this. Possible problems here:
The semaphore is incorrect.
The mode for fopen() is incorrect. See PHP docs. The mode w is for write. The tag b is for "If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data ...".
Something weird happening because I use read() in my writing function?
Code:
// Write $val to $obj in database JSON
function write($obj,$val){
$content = read();
$json = json_decode($content);
$json->{$obj} = $val; // eg. $json->{'id'} = 5;
$myfile = fopen("database.txt", "wb") or die("Unable to open file!");
if(flock($myfile, LOCK_EX|LOCK_NB)) {
fwrite($myfile,json_encode($json));
flock($myfile, LOCK_UN);
}
fclose($myfile);
}
Code Piece 2: This is my code to send data. It is called via a setInterval(). In script.js:
// Send message to server.php, call callback with answer
function communicate(messageFunc,callback){
var message = messageFunc();
if (window.XMLHttpRequest) {
var xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange= function() {
if (this.readyState==4 && this.status==200) {
callback(this.responseText);
}
}
xmlhttp.open("GET","server.php?msg="+message,true);
xmlhttp.send();
}
This is my code to receive data, in server.php: $receive = $_GET["msg"].
My current work of solving
This is what I have done so far, but nothing has changed:
Added mode b to fopen().
Added flock() to read/write functions in server.php.
Much reworking on script.js, I would say it looks/works very clean.
Check memory_get_peak_usage(), and check with the hosting company for memory limits. Should be no problem at all.
Looked at PHP garbage collecting and gc_enable() (I don't know why that would change anything).
Lots of testing, looking at the data flow.
Crying.
Conclusion: Is this type of application what PHP is for? What do you think is wrong? If you want more code/info I provide. Thank you very much.
Here is the root of your problem:
$myfile = fopen("database.txt", "wb") or die("Unable to open file!");
Note the behavior of the w open mode (emphasis mine):
Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
This happens before you lock the file. What's happening is that between this fopen() call and the following flock() call, the file's content is zero length, and a reader is coming along during that time and reading the empty file.
Why doesn't this cause an error in PHP when you parse the empty string as JSON? Because json_decode() is defective, and returns null when the input is not valid JSON rather than throwing an exception. Nevermind that the string "null" is valid JSON -- json_decode() gives you no way to differentiate between the cases of valid input representing the null value and invalid input. If json_decode() actually threw an exception or triggered a PHP error (don't ask me why two error-signalling mechanisms are necessary in PHP), you would have a fantastic point to start debugging to figure out why the file is empty, and you might have solved this problem by now!
... sigh ...
PHP's "design" gives me headaches. But I digress.
To fix this whole problem, change the open mode to "cb" and ftruncate($myfile, 0) after you successfully acquire the lock.
Note the behavior of the c mode, which actually specifically mentions the approach you are using (emphasis mine):
Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).

PHP urlencoded variable becomes undefined?

I have a html page with some buttons, one of my buttons is as following:
<input class="contentLink" type="button" value="Questionnaire" onclick="validate(<?php urlencode('/my-site/Tools/Learning_Strategies/Questionnaire.php'); ?>)"/>
Essentially when this button is clicked, it passes the urlencoded-string literal '/my-site/Tools/Learning_Strategies/Questionnaire.php' into the javascript method validate:
function validate(url){
alert(url);
location.href="/my-site/Session/redirectMe.php?loc="+url;
}
What I am trying to do is get '/my-site/Session/Questionnaire.php' from the html page accessible from the redirectMe.php. This is being setup in the
location.href="/my-site/Session/redirectMe.php?loc="+url;
which leads to my redirectMe.php file:
<?php
require "SessionAuthenticator.php";
Session_start();
if(validateSession()===true){//validateSession() is defined inside SessionAuthenticator.php
echo $_GET["loc"];
//header("Location: ".$_GET["loc"]);
}else{
header("Location: /my-site/LoginPage/index.php");
}
?>
The issue is during both of the alert and echo, the encoded string literal came out as "undefined", which means the following line:
header("Location: ".$_GET["loc"]);
will really look like this:
header("Location: undefined");
And therefore look for 'undefined' in the current directory and break the website.
This all works perfectly if there is no encoding as following:
<input class="contentLink" type="button" value="Questionnaire" onclick="validate('/my-site/Tools/Learning_Strategies/Questionnaire.php')"/>
But... No encoding is bad right?
If you are wondering why you don't see a decode method being used...
The webserver will arrange for $_GET to have been urldecoded once already by the time it reaches you! - Matt Johnson ¶ # http://php.net/manual/en/function.urldecode.php#48481
Require and include will not work, as redirectMe.php would be executed before the user even sees the page. This should only happen when the user clicks the button. That is why I am using a javascript function to start redirectMe.php.
I've tried ajax, however this didn't work. After talking with an experienced programmer who I know personally, I've been told that ajax can't help me here. This because Ajax would be redirected to '/my-site/Tools/Learning_Strategies/Questionnaire.php', but not the client (User).
To clearly state my question:
Why is the urlencoded coming up as 'undefined' when echo-ed/alert-ed? How do I fix this?
As for programs/web servers/etc I am using...
Sublime 3 (text editor)
Wamp (64BIT)(all-in-one package) # http://www.wampserver.com/en/
Apache 2.4.17
PHP 7
MySQL 5.7.9
I am open to other means of getting what I am trying to do done.
However, I am not open to changing web servers/programs etc (except sublime 3 since it is just a text editor.... but why would that need to be required?).
Furthermore, whatever means of getting this done needs to support the browsers/platforms as stated below.
The website must support:
(Browsers)
IE 8 and up, and latest versions of Microsoft Edge, Firefox, Chrome and Safari.
Cross-Platform:
Windows 7 and up, and the latest versions of Mac OS, IOS and Android
Why is the urlencoded coming up as 'undefined' when echo-ed/alert-ed?
Because the PHP doesn't output anything.
<input class="contentLink" type="button" value="Questionnaire" onclick="validate(<?php urlencode('/my-site/Tools/Learning_Strategies/Questionnaire.php'); ?>)"/>
Gives you:
<input class="contentLink" type="button" value="Questionnaire" onclick="validate()"/>
… once you run it through a PHP parser.
You need to:
echo the result so something appears there
wrap it in quotes (with json_encode so any escaping needed is also taken care of) so that it will be treated as a string and not a variable name.

Using combination of JSON.stringify and encodeURIComponent

So I hava a JSON stored in a variable
var x = '{"time":"Friday, January 15, 2016 9:13 PM","keywords":"social","tweets_people":[{"text":"#FreeformTV I want to see what my social DNA looks like! #FreeformLaunch","username":"bbjeagle","user_image":"http://pbs.twimg.com/profile_images/681285613526515712/H6aB4gS__normal.jpg","timestamp":"2016-01-15T15:42:59.000Z","display_name":"JOY","language":"en","tweet_id":"688023580240670720","location":"united_states","location_details":{"country":"united_states","type":"country"},"retweet_count":0,"reach":531,"favorite_count":0,"sentiment":84,"entities":{}},{"text":"#IBM Scoops Up IRIS Analytics Fraud Detection Firm https://t.co/6oisTfWGNq #technology #enterprise #social #tc","username":"chriswrighttech","user_image":"http://pbs.twimg.com/profile_images/577224176542445568/1N2FMNHV_normal.jpg","timestamp":"2016-01-15T15:42:55.000Z","display_name":"Chris Wright","language":"en","tweet_id":"688023567221534721","location":null,"location_details":null,"retweet_count":0,"reach":458,"favorite_count":0,"sentiment":-50,"entities":{"urls":[{"display_url":"goo.gl/fb/weklj1","expanded_url":"http://goo.gl/fb/weklj1","url":"https://t.co/6oisTfWGNq"}]}},{"text":"Handbook of Statistical Modeling for the Social and Behavioral Sciences https://t.co/qCbResQLzK https://t.co/sLf2uhhUQd","username":"allwood_mary","user_image":"http://pbs.twimg.com/profile_images/653044576501035008/VWEwjp_N_normal.jpg","timestamp":"2016-01-15T15:42:49.000Z","display_name":"Mary Allwood","language":"en","tweet_id":"688023541023924227","location":null,"location_details":null,"retweet_count":0,"reach":52,"favorite_count":0,"sentiment":34,"entities":{"urls":[{"display_url":"uae-trip.info/p/tr/?query=ht…","expanded_url":"http://uae-trip.info/p/tr/?query=http://rover.ebay.com/rover/1/711-53200-19…=2&toolid=10039&campid=5337597384&item=151951539608&vectorid=229466&lgeo=1","url":"https://t.co/qCbResQLzK"}],"photo":[{"display_url":"pic.twitter.com/sLf2uhhUQd","expanded_url":"http://twitter.com/allwood_mary/status/688023541023924227/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxZw96WsAAzzvk.jpg","resolution":{"w":242,"h":400,"resize":"fit"},"url":"https://t.co/sLf2uhhUQd"}]}},{"text":"Philasmicos R5 CRM (Standard) #Business Social Networking #Mac App... https://t.co/88YCSejyyE #socialnetworking","username":"marac00per","user_image":"http://pbs.twimg.com/profile_images/509110528208142336/Ah4ZC3vh_normal.jpeg","timestamp":"2016-01-15T15:42:42.000Z","display_name":"Mara Cooper","language":"en","tweet_id":"688023510111916032","location":"miami","location_details":{"country":"united_states","city":"miami","state":"florida","type":"city"},"retweet_count":0,"reach":33649,"favorite_count":0,"sentiment":0,"entities":{"urls":[{"display_url":"goo.gl/fb/7hwwhZ","expanded_url":"http://goo.gl/fb/7hwwhZ","url":"https://t.co/88YCSejyyE"}]}}],"tweets_media":[{"text":"The Peach app is dead, but it taught us this about social media: https://t.co/0IJsSnb2z2 https://t.co/iqu71DKk4L","username":"NewRepublic","user_image":"http://pbs.twimg.com/profile_images/631195731756388352/rXgYnC9S_normal.jpg","timestamp":"2016-01-15T15:40:22.000Z","display_name":"New Republic","language":"en","tweet_id":"688022921747533824","location":"washington","location_details":{"country":"united_states","state":"washington","type":"state"},"retweet_count":0,"reach":139506,"favorite_count":0,"sentiment":35,"entities":{"urls":[{"display_url":"bit.ly/234xz4y","expanded_url":"http://bit.ly/234xz4y","url":"https://t.co/0IJsSnb2z2"}],"photo":[{"display_url":"pic.twitter.com/iqu71DKk4L","expanded_url":"http://twitter.com/NewRepublic/status/688022921747533824/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxZM7FWMAMxrSz.jpg","resolution":{"w":1024,"h":532,"resize":"fit"},"url":"https://t.co/iqu71DKk4L"}]}},{"text":"FG will ensure social welfare for the poor – Osinbajo https://t.co/KDwyJnbAgj via #todayng https://t.co/BPpQ7GE4Gi","username":"NigeriaNewsdesk","user_image":"http://pbs.twimg.com/profile_images/559635895909752832/5m7oAlGi_normal.png","timestamp":"2016-01-15T15:39:37.000Z","display_name":"Nigeria Newsdesk","language":"en","tweet_id":"688022736917118980","location":null,"location_details":null,"retweet_count":2,"reach":1070269,"favorite_count":0,"sentiment":0,"entities":{"urls":[{"display_url":"today.ng/news/national/…","expanded_url":"https://www.today.ng/news/national/66295/fg-will-ensure-social-welfare-for-the-poor-osinbajo?utm_source=dlvr.it_nnd&utm_medium=twitter","url":"https://t.co/KDwyJnbAgj"}],"photo":[{"display_url":"pic.twitter.com/BPpQ7GE4Gi","expanded_url":"http://twitter.com/NigeriaNewsdesk/status/688022736917118980/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxZCKsWYAI0NZw.jpg","resolution":{"w":1024,"h":806,"resize":"fit"},"url":"https://t.co/BPpQ7GE4Gi"}]}},{"text":"Building your personal brand on social media is as much about listening as talking. https://t.co/U8VwyRYgD6 https://t.co/1qes1tfVMB","username":"FT","user_image":"http://pbs.twimg.com/profile_images/466972537704824832/eflEColL_normal.png","timestamp":"2016-01-15T15:09:50.000Z","display_name":"Financial Times","language":"en","tweet_id":"688015237958774784","location":null,"location_details":null,"retweet_count":0,"reach":2190381,"favorite_count":0,"sentiment":35,"entities":{"urls":[{"display_url":"on.ft.com/234vi9l","expanded_url":"http://on.ft.com/234vi9l","url":"https://t.co/U8VwyRYgD6"}],"photo":[{"display_url":"pic.twitter.com/1qes1tfVMB","expanded_url":"http://twitter.com/FT/status/688015237958774784/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxSNpKWMAArwFh.jpg","resolution":{"w":600,"h":338,"resize":"fit"},"url":"https://t.co/1qes1tfVMB"}]}},{"text":"Who has decided that women cannot go in?: Ranjana Kumari, Director, Centre for Social Research on #SabrimalaRow #LRC_NDTV","username":"ndtv","user_image":"http://pbs.twimg.com/profile_images/570440108424171520/QuGYd7jH_normal.png","timestamp":"2016-01-15T14:46:17.000Z","display_name":"NDTV","language":"en","tweet_id":"688009314565550080","location":"india","location_details":{"country":"india","type":"country"},"retweet_count":15,"reach":5824509,"favorite_count":6,"sentiment":0,"entities":{}}],"topUsers":[["FreeformTV",678,"100%"],["AsMickymouse82",141,"-100%"],["ILoveMyDinoMatt",120,"0%"],["CloudataNow",79,"12%"],["TheSocialMs",79,"100%"],["mhsowrove",75,"100%"],["TSM_B2B",70,"100%"],["ExploreWeb2dot0",61,"-4%"],["Go2WebMarketing",61,"-4%"],["dreckbaerfrau",58,"100%"]],"locations":[["london",523],["new_york",321],["chicago",238],["los_angeles",204],["toronto",156],["kota",152],["atlanta",129],["miami",121],["mumbai",106],["boston",104]],"time_series_mentions":{"14/01/2016_15:43":1501,"14/01/2016_16:43":1592,"14/01/2016_17:43":1692,"14/01/2016_18:43":1477,"14/01/2016_19:43":1537,"14/01/2016_20:43":1864,"14/01/2016_21:43":1313,"14/01/2016_22:43":1448,"14/01/2016_23:43":1498,"15/01/2016_00:43":1289,"15/01/2016_01:43":1094,"15/01/2016_02:43":1313,"15/01/2016_03:43":1337,"15/01/2016_04:43":1075,"15/01/2016_05:43":1069,"15/01/2016_06:43":1053,"15/01/2016_07:43":808,"15/01/2016_08:43":844,"15/01/2016_09:43":524,"15/01/2016_10:43":973,"15/01/2016_11:43":1148,"15/01/2016_12:43":1313,"15/01/2016_13:43":1272,"15/01/2016_14:43":1277},"time_series_impressions":{"14/01/2016_15:43":9631791,"14/01/2016_16:43":24597649,"14/01/2016_17:43":42674232,"14/01/2016_18:43":61093552,"14/01/2016_19:43":70688408,"14/01/2016_20:43":47669000,"14/01/2016_21:43":24028898,"14/01/2016_22:43":76327602,"14/01/2016_23:43":50294599,"15/01/2016_00:43":74220157,"15/01/2016_01:43":45603520,"15/01/2016_02:43":29450814,"15/01/2016_03:43":46223938,"15/01/2016_04:43":15439446,"15/01/2016_05:43":19868983,"15/01/2016_06:43":82828364,"15/01/2016_07:43":98291003,"15/01/2016_08:43":11781541,"15/01/2016_09:43":8872385,"15/01/2016_10:43":4314408,"15/01/2016_11:43":14093326,"15/01/2016_12:43":10846241,"15/01/2016_13:43":8302175,"15/01/2016_14:43":18581107},"time_series_sentiment":{"14/01/2016_15:43":60,"14/01/2016_16:43":67,"14/01/2016_17:43":55,"14/01/2016_18:43":69,"14/01/2016_19:43":69,"14/01/2016_20:43":61,"14/01/2016_21:43":61,"14/01/2016_22:43":58,"14/01/2016_23:43":54,"15/01/2016_00:43":61,"15/01/2016_01:43":68,"15/01/2016_02:43":60,"15/01/2016_03:43":59,"15/01/2016_04:43":57,"15/01/2016_05:43":50,"15/01/2016_06:43":70,"15/01/2016_07:43":70,"15/01/2016_08:43":61,"15/01/2016_09:43":73,"15/01/2016_10:43":66,"15/01/2016_11:43":68,"15/01/2016_12:43":70,"15/01/2016_13:43":66,"15/01/2016_14:43":67},"relatedTopics":[["#Waze",1745,-76.9],["#Freeformtv",1229,100],["#Freeformlaunch",1215,100],["#Gopdebate",693,-14.1],["#Social",668,45.2],["#Arsenal",493,-99.2],["#Elnennym",490,-100],["#Socialmedia",364,66.4],["#Marketing",261,62.2],["#Realdonaldtrump",236,69],["#Gurmeetramrahim",223,100]],"media":{"mentions":145,"impressions":230059822,"sentiment":"20%"},"people":{"mentions":30311,"impressions":895723139,"sentiment":"25.2%"}}';
I know this is avalid JSON (check here)
Now when I try to pass it to encodeURIComponent() to make it URL Safe, it doesn't encode it properly. I don't know why is it happening.
I want to pass this JSON to a PHP file from which i want to genrate a Excel file (genrated using this JSON data) using PHPExcel. I am doing this so that I can redirect the user to the php page passing this JSON as a GET parameter and which will download the file (as default behaviour of PHPExcel is it stores the file in filesystem) directly to the client without storing it to the filesystem.
By the way is it doable using AJAX, making an AJAX call to the PHP file passing this JSON as a POST data and, the PHP file builds EXCEL and respond with the excel file without uploading it to the server so that it can be directly downloaded to the client.
Your string is 8951 long before encoding, read this post to see why your GET query fails. Change your Ajax call to POST and that will work.
// x is not a string anymore, but a JSON object
var x= {"time":"Friday, January 15, 2016 9:13 PM", .... } ;
$.post ('myurl.php', x, function (ret, status) { ... }) ;

Using Javascript to make parallel server requests THREDDS OPeNDAP

For the following THREDDS OPeNDAP server:
http://data.nodc.noaa.gov/thredds/catalog/ghrsst/L2P/MODIS_T/JPL/2015/294/catalog.html
I would like to note four Attributes of every file in there. The attributes are:
northernmost lattitude; easternmost lattitude; westernmost lattitude; southernmost lattitude. These can be found under the Global attributes under:
http://data.nodc.noaa.gov/thredds/dodsC/ghrsst/L2P/MODIS_T/JPL/2015/294/20151021-MODIS_T-JPL-L2P-T2015294235500.L2_LAC_GHRSST_N-v01.nc.bz2.html
At first I tried this with MATLAB. Problem is: all the netcdf files on the server are compiled to .bz2 files. This makes calling for the Global attributes take around 15 seconds (the server is extracting the file). I would like javascript to run these server requests parallel to save me time. In total I need 90,000 files.
Is there a way to code this using javascript?
You can use the THREDDS DAS service. DAS
Change the OPenDAP link you have above replacing the .html extension with .das
This is a small text file with metadata about the file which could be easily parsed with javascript and includes a section with the global attributes:
NC_GLOBAL {
. . .
Float32 northernmost_latitude 89.9942;
Float32 southernmost_latitude 66.9853;
Float32 easternmost_longitude -121.445;
Float32 westernmost_longitude 76.7485;
. . .
}
This metadata is cached by THREDDS and the above DAS link responds instantly.
Edit:
Re: the correct comments below, (cache exists only after the first request) one alternative might be to use the source data at the NASA JPL OPeNDAP Server (Hyrax): http://podaac-opendap.jpl.nasa.gov/opendap/allData/ghrsst/data/L2P/MODIS_T/JPL/
My browser only tests (i.e. subjective) seem to show that a random DAS responses are quicker, than 15 seconds.
http://podaac-opendap.jpl.nasa.gov/opendap/allData/ghrsst/data/L2P/MODIS_T/JPL/2015/294/20151021-MODIS_T-JPL-L2P-T2015294084500.L2_LAC_GHRSST_N-v01.nc.bz2.das

How can I get image data from a server side perl program and display in a div using javascript

I have done a lot of searching and either the answer doesn't work or it's not a good solution. The solution of just putting a reference into the html like:
<img src="html://localhost/cgi-bin/fd.cgi"/>
Has two problems, one is the image will change and it'll get cached. I know that I can put a random parameter on the call and get around the problem, but the other problem is that the program runs for a few seconds. I use javascript, setInterval, to loop. The perl program reads an image file and annotates it. This take a while, 3 or 4 seconds and the display blanks out during that time, so I have two divs, one is hidden while the other is being displayed. The logic is that I'll load the hidden one with an image and when the load is complete, I'll hide the first and display the second, next pass, vice versa. There seems to be no event I can trap at "load completion". None I saw in the debugger at any rate.
The ajax call brings in the data but I can't figure out how to display it. I base64 encode it in the perl script. Here's the javascript.
$.ajax({ type: 'GET',
url: 'http://localhost/cgi-bin/fdmap/image.pl',
success: function(data)
{
$('#img1').html('<img src="data:image/jpg;base64,'+data+'"');
},
error: function(data)
{
console.log("Error ");
return true;
},
});
html:
<div id="img1"></div>
<div class="hidden" id="img2"></div>
Here's the perl snippet
binmode STDOUT;
print $cgi->header("image/jpg;base64");
open INP,"pcb.jpg";
my $it;
while(<INP>) {
my $in = $_;
$it.=$in;
}
my $encoded = encode_base64($it);
print $encoded;
I'm just using a small jpg file for testing. Once I figure out how to display the image data I"ll generate the image dynamically.
When I run the perl script from the console I get the following (truncated) output.
Content-Type: image/jpg;base64^M
^M
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEP
ERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4e
Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAF9AZwDASIA
If I look at the data received via the javascript debugger I see:
HTTP/1.1 200 OK
Date: Sun, 05 May 2013 15:28:43 GMT
Server: Apache/2.2.14 (Ubuntu)
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: image/jpg;base64
And the data in the response looks the same as the data dumped on the console.
However nothing is displayed and there are no error messages to be seen. Just a blank screen. I've been working on this for many hours. Thanks for any help you can give me.
Oh and I could care less if it works in IE or not.
Only with quick scan, so i'm not sure, but - are you sure than
$('#img1').html('<img src="data:image/jpg;base64,'+data+'"');
^
//not missing the tag-end?---+
because you get
<img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASAB....."
and need (IMHO)
<img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASAB....." />

Categories