views:

457

answers:

3

I would like to fetch a source of file and wrap it within JSONP.

For example, I have pets.txt. I want to retrieve source of that file from another domain using nothing but client-side JavaScript. Can I do it?

Actually, I can't. I can do it only for JSONP. So, I can convert pets.txt to JSONP.

I'm looking for online service which can convert anything to JSONP.


YQL

Yahoo Query Language is one of them.

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D"http://elv1s.ru/x/pets.txt"&format=json&callback=grab

This works if URL is not blocked by robots.txt. YQL have respect to robots.txt. I can't fetch http://userscripts.org/scripts/source/62706.user.js because it blocked via robots.txt.

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D"http://userscripts.org/scripts/source/62706.user.js"&format=json&callback=grab

"forbidden":"robots.txt for the domain disallows crawling for url: http://userscripts.org/scripts/source/62706.user.js"


So I'm looking for another solutions.

+1  A: 

I built JSONPwrapper.com.

It's unstable and slower than YQL, but it doesn't care about robots.txt.

NV
once enough users use your wrapper, will you go nuts and hack everyone's site ;) ?
Luca Matteis
A: 

I'm not sure what you're trying to do here, but nobody will use something like this. Nobody is going to trust your service to always execute as it should and output expected JavaScript code. You see Yahoo doing it because people trust Yahoo, but they will not trust you.

Luca Matteis
Get source code (http://github.com/NV/jsonpwrapper.com/) and host it yourself if you like.
NV
Lay off with the trolling already.
Brian Cline
A: 

Nononono. No. Just please; no. That is not JSONP, it is javascript that executes a function with an object as its parameter that contains more javascript. Aaah!

This is JSON because it's just one object:

{
    'one': 1,
    'two': 2,
    'three':3
}

This is JSONP because it's just one object passed through a function; if you go to http://somesite/get_some_object?jsonp=grab, the server will return:

grab({
    'one': 1,
    'two': 2,
    'three':3
});

This is not JSON at all. It's just Javascript:

alert("hello");

And this? Javascript code stored inside a string (ouch!) inside an object passed to a function that should evaluate the string (but it might or might not):

grab({"body": "alert(\"Hello!\");\n"});

Look at all those semicolons and backslashes! I get nightmares from this kind of stuff. It's like a badly written Lisp macro because it's much more complicated than it needs to (and should!) be. Instead, define a function called grab in your code:

function grab(message) {
    alert(message.body);
}

and then use JSONP to have the server return:

grab({body: "Hello!"});

Don't let the server decide how to run your web page Instead, let your web page decide how to run the web page and just have the server fill in the blanks.

As for an online service that does this? I don't know of any, sorry

Michael
Oh, my example is really bad. I've replaced http://elv1s.ru/x/hello.js with http://elv1s.ru/x/hi.txt
NV
OHHHHHHHHHHHH oh oh oh! You're just trying to wrap a file, not more javascript. Sorry about that mean little lecture, I see it now.
Michael