YUI and json data - jsonp

I have been trying out the YUI library and got stuck with this code below based on:
http://yuilibrary.com/yui/docs/jsonp/index.html
I have stored some localhost json data, which does validate and the file name is data.json and the data is below:
[
{
"created_at":"Wed Nov 28 23:13:00 +0000 2012",
"id":273927502659981312,
"id_str":"273927502659981312",
"text":"Get INTO this Season 5 promo for Drag Race - before Viacom sics their copyright-nazis on me. It's sickening.... http:\/\/t.co\/a6Ld4mKN",
"source":"\u003ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003eFacebook\u003c\/a\u003e",
"truncated":false,
"in_reply_to_status_id":null,
"in_reply_to_status_id_str":null,
"in_reply_to_user_id":null,
"in_reply_to_user_id_str":null,
"in_reply_to_screen_name":null,
"user":{
"id":8394862,
"id_str":"8394862",
"name":"mralexgray",
"screen_name":"mralexgray",
"location":"NYC",
"url":"http:\/\/mrgray.com",
"description":"Fierceness Incarnate",
"protected":false,
"followers_count":129,
"friends_count":385,
"listed_count":0,
"created_at":"Fri Aug 24 01:00:57 +0000 2007",
"favourites_count":7,
"utc_offset":-18000,
"time_zone":"Quito",
"geo_enabled":true,
"verified":false,
"statuses_count":147,
"lang":"en",
"contributors_enabled":false,
"is_translator":false,
"profile_background_color":"53777A",
"profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/88810365\/x86b7f9c12df0fa38ce1a4f29b759706.png",
"profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/88810365\/x86b7f9c12df0fa38ce1a4f29b759706.png",
"profile_background_tile":false,
"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2563994027\/2ls5b34rje2nrkqriw2i_normal.png",
"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2563994027\/2ls5b34rje2nrkqriw2i_normal.png",
"profile_link_color":"C02942",
"profile_sidebar_border_color":"542437",
"profile_sidebar_fill_color":"ECD078",
"profile_text_color":"D95B43",
"profile_use_background_image":true,
"default_profile":false,
"default_profile_image":false,
"following":null,
"follow_request_sent":null,
"notifications":null
},
"geo":null,
"coordinates":null,
"place":null,
"contributors":null,
"retweet_count":0,
"entities":{
"hashtags":[
],
"urls":[
{
"url":"http:\/\/t.co\/a6Ld4mKN",
"expanded_url":"http:\/\/fb.me\/1IJWfEnth",
"display_url":"fb.me\/1IJWfEnth",
"indices":[
113,
133
]
}
],
"user_mentions":[
]
},
"favorited":false,
"retweeted":false,
"possibly_sensitive":false
}
]
I then have this simple html file with the code as follows:
<!DOCTYPE html>
<html>
<head>
<script src="http://yui.yahooapis.com/3.8.0/build/yui/yui-min.js"></script>
<script>
// Create a new YUI instance and populate it with the required modules.
YUI().use('jsonp', 'jsonp-url', 'node' , function (Y) {
// JSONP is available and ready for use. Add implementation
// code here.
var url = "http://localhost/yui/data.json?callback={callback}";
function handleJSONP(response) {
// response is a JavaScript object. No parsing necessary
console.log(response);
Y.one('#output').setHTML(response.outputHTML);
}
Y.jsonp(url, handleJSONP);
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
The console produces no output and the div tag contains no output, does anyone know why?

The url you're requesting is for the file data.json, which the server will reply with the file contents. What you need is to make a request to a url that is handled by server code that reads the query parameter named "callback", and replies with "{callback}({content of data.json});".
For example, you would write a getData.php that handles a request to "http://localhost/yui/getData.php?callback=foo.bar.baz" with the response foo.bar.baz([{"created_at":…<the rest of data.json>…]);.
If the page that's accessing the data is on the same domain as the data, then use Y.io() + Y.JSON.parse() rather than Y.jsonp().

Related

Blank pdf output using jsreport

I know this has got to have some super easy answer but I am just beginning with this stuff and followed this tutorial: https://jsreport.net/blog/pdf-reports-in-sql-server
Except it gives me a blank page as an output with no real error message.
I'm using handlebars and chrome-pdf to do this...
My database connection script is:
const sql = require('mssql');
const config = {
"user": "user",
"password": "password",
"server": "server",
"database": "database"
}
async function beforeRender(req, res) {
await sql.connect(config)
const sqlReq = new sql.Request();
const recordset = await sqlReq.query(
`SELECT DBVersion
,MinAppVersion
FROM VersionTbl`
)
Object.assign(req.data, {Versions: recordset });
}
I can see it running and connecting properly in the debug tab... and my sql query seems to be correct if I test it directly in the database server.
My template looks like this:
<table>
{{#each Version}}
<tr>
<td>{{DBVersion}}</td>
<td>{{MinAppVersion}}</td>
</tr>
{{/each}}
</table>
The debug log...
+0 Starting rendering request 27 (user: null)
+2 Rendering template { name: PQRTemplate, recipe: chrome-pdf, engine: handlebars, preview: true }
+2 Data item not defined for this template.
+9 Resources not defined for this template.
+10 Executing script Connection using dedicated-process strategy
+779 Base url not specified, skipping its injection.
+780 Rendering engine handlebars using dedicated-process strategy
+937 Compiled template not found in the cache, compiling
+951 Executing recipe chrome-pdf
+1041 Converting with chrome HeadlessChrome/79.0.3945.0 using dedicated-process strategy
+1115 Page request: GET (document) file:///C:/Users/********/AppData/Local/Temp/jsreport/autocleanup/264c975a-9ef2-4130-960c-84eeae2ec04a-chrome-pdf.html
+1122 Page request finished: GET (document) file:///C:/Users/*******/AppData/Local/Temp/jsreport/autocleanup/264c975a-9ef2-4130-960c-84eeae2ec04a-chrome-pdf.html
+1124 Running chrome with params {"printBackground":true,"margin":{}}
+1327 Skipping storing report.
+1327 Rendering request 27 finished in 1327 ms
Can any of you smart people tell me what's going wrong? Thanks
See the answer from Jan_blaha here... https://forum.jsreport.net/topic/1814/blank-pdf-output
It perfectly solved my issue with the tutorial.

POSTMAN COLLECTION RUNNER: passing the all body post request as a variable?

I have a POST request template - instead putting all the JSON body i put a variable: {{bodyRequest}}
now i have a json file that looks like this(Below code bloc) that i upload in my collection runner as data json: my intention is to fetch {{bodyResponse}} from the iteration and make the post call in the api.
[
{ "{{bodyRequest}}":
{
"order": {
"order_date":"{{$timestamp}}",
"order_number":"{{$guid}}",
"reference":"Scenario1",
"destination":
{
"name":"test",
"phone":"00000",
"email":"test#test.com",
"company":"My Company",
"building":"My Building",
"street":"my street",
"suburb":"my sububr",
"post_code":"my postcode",
},
}
}
}
]
Then i click run
Open Collection Runner
Add Data file in Format JSON (above in code)
declare it as Json
run collection
Error Log
Request Body:"{{bodyRequest}} "
instead the all payload
thank you so much
Franco

Using Google Analytics on a 503 page

I'm using varnish to cache our pages. When we get a 503 -- which happens a little too often -- I'd like to put some sort of page tracking on there. I'd like to place the GA code in there. I can't seem to find any instance of anyone else doing this. Has anyone done this? Is there some sort of T&C violation in doing this?
For Varnish you can use vcl_error to include your own responses (that have the Google Analytics code).
Edit: I have not tested any of these. They are just examples.
An example:
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
if (obj.status == 503) {
synthetic {"
<html>
<head>
<title></title>
</head>
<body>
<h1>Error</h1>
<p>Something</p>
<!-- ANALYTICS CODE -->
</body>
</html>
"};
return(deliver);
}
}
Alternatively you can add your own pages from the file system by using vmod (which is included as standard with version 3.*+).
# Add this to the top of your config
import std;
# vcl_error
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
if (obj.status == 503) {
set obj.http.error503 = std.fileread("/path/to/errors/503.html");
synthetic obj.http.error503;
return(deliver);
}
}

Rails JSON unexpected token error

I'm perplexed by the issues I'm seeing with my JSON. I am using my controller to inject some values, read from a file from my public folder, into a fxn call.
app/views/comparison/graphs.html.haml:
- tmp = File.readlines(txt)
:javascript
generate_plot(#{tmp.first})
which generates this in my html:
<script>
//<![CDATA[
generate_plot([[[22.0,307.0,491.0,35.0,580.0,40.0,387.0,28.0,34.0,519.0,4.0,59.0,33.0,448.0,24.0,341.0],["2011-04-27T05:32:00-06:00","2011-04-27T05:32:00-06:00","2011-05-20T02:49:00-06:00","2011-05-20T02:49:00-06:00","2010-10-12T23:27:00-06:00","2010-10-12T23:27:00-06:00","2010-11-21T19:20:00-07:00","2010-11-21T19:20:00-07:00","2011-11-03T08:59:00-06:00","2011-11-03T08:59:00-06:00","2011-02-22T03:24:00-07:00","2011-02-22T03:24:00-07:00","2010-08-17T20:26:00-06:00","2010-08-17T20:26:00-06:00","2011-11-09T06:15:00-07:00","2011-11-09T06:15:00-07:00"]],[[24.0,463.0,566.0,31.0,864.0,57.0,28.0,596.0,717.0,47.0,31.0,28.0,596.0,35.0,597.0,622.0,463.0,24.0,463.0,24.0,719.0,51.0,556.0,30.0,51.0,891.0,30.0,556.0,494.0,38.0,23.0,443.0,581.0,28.0,581.0,28.0,556.0,30.0,622.0,31.0],["2010-10-15T10:04:00-06:00","2010-10-15T10:04:00-06:00","2010-09-20T04:24:00-06:00","2010-09-20T04:24:00-06:00","2010-01-08T02:10:00-07:00","2010-01-08T02:10:00-07:00","2010-09-02T20:40:00-06:00","2010-09-02T20:40:00-06:00","2010-07-08T13:11:00-06:00","2010-07-08T13:11:00-06:00","2010-02-22T23:10:00-07:00","2010-09-15T04:13:00-06:00","2010-09-15T04:13:00-06:00","2010-05-08T19:01:00-06:00","2010-05-08T19:01:00-06:00","2010-02-22T23:10:00-07:00","2010-01-10T18:59:00-07:00","2010-01-10T18:59:00-07:00","2010-06-23T14:10:00-06:00","2010-06-23T14:10:00-06:00","2010-04-04T21:57:00-06:00","2010-04-04T21:57:00-06:00","2010-01-09T11:01:00-07:00","2010-01-09T11:01:00-07:00","2010-04-30T02:10:00-06:00","2010-04-30T02:10:00-06:00","2010-10-08T13:59:00-06:00","2010-10-08T13:59:00-06:00","2010-03-03T13:12:00-07:00","2010-03-03T13:12:00-07:00","2010-06-29T15:06:00-06:00","2010-06-29T15:06:00-06:00","2010-02-14T05:47:00-07:00","2010-02-14T05:47:00-07:00","2010-06-18T05:23:00-06:00","2010-06-18T05:23:00-06:00","2010-05-12T20:05:00-06:00","2010-05-12T20:05:00-06:00","2010-03-04T19:18:00-07:00","2010-03-04T19:18:00-07:00"]],2])
//]]>
</script>
I've checked and rechecked my input, but I get this error:
Unexpected token error ,
My Json is valid, when I simply delete the parenthesis and the function call:
http://jsfiddle.net/VVL8C/
I'm not sure what else could be going wrong.
Thanks
Here is a valid JSON example
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
http://www.json.org/js.html
At the end instead of ,2]) try
,[[2],[""]] )

Using Google's ClientLogin Interface via XMLHttpRequest in Javascript

I am trying to learn the ClientLogin Interface detailed on the Account Authentication APIs on Google code website.
I am using Firefox 3.5pre (Shiretoko) and XMLHttpRequest object in Javascript to follow the process. Here's a stripped down version of what I have:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
//<![CDATA[
function update() {
var auth_params = "accountType=HOSTED_OR_GOOGLE&Email=val"
+"&passwd=val&service=cl&source=MMA-Learning";
var request = new XMLHttpRequest();
request.open('POST', 'https://www.google.com/accounts/ClientLogin', true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-Length", auth_params.length);
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
alert ("Request done");
}
};
try {
request.send( auth_params );
} catch (e) {
alert ("Send Exception:\n"+e);
}
}
//]]>
</script>
</head>
<body>
Authenticate
</body>
</html>
When I click on the Authenticate link, all I get back is a Bad Request response. Examining the request headers, I don't see Content-Type set to application/x-www-form-urlencoded.
I am using Firebug 1.5X to examine the traffic.
For now, all I want to do is generate request mentioned in the Sample Request section and get a response mentioned in the Sample Responses section. If I get there, I want to get some account specific data like, unread Google Reader feeds etc.
I suspect that you've been bitten by Javascript's "same origin" policy. It prevents Javascript, including XmlHttpRequest, from accessing one domain from another. More information is available from Mozilla.
There are hacks to get around this, but I have no idea if they'll work with Google's API.
the 'p' in 'passwd' is a small 'p' instead of a capital 'P'
you probably figured that out tho. When you post and you find the answer, it is always polite if you post the answer as well. This helps the people in the future who will look at your post for information
That 'p' took me two hours to find because i persummed that the code google gave was copied correctely and there was no case mistakes
no point in Internet being full of questions with no answers