6

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

For example, I want to retrieve pets.txt as text from a host I don't own. I want to do that by using nothing but client-side JavaScript.

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.

NVI
  • 14,907
  • 16
  • 65
  • 104
  • What do you mean "there is no such jsonpwrapper.com"? – Luca Matteis Jan 26 '10 at 19:02
  • I've updated question. I hope now it sounds more reasonable. – NVI Jan 26 '10 at 20:05
  • This is a duplicate of my question that Will asserted is not constructive: [Is there a free JSON proxy server which supports CORS or JSONP?](http://stackoverflow.com/questions/8537601) – hippietrail Jun 02 '13 at 04:56
  • possible duplicate of [Is there a free JSON proxy server which supports CORS or JSONP?](http://stackoverflow.com/questions/8537601/is-there-a-free-json-proxy-server-which-supports-cors-or-jsonp) – hippietrail Jun 02 '13 at 04:57

4 Answers4

5

I built jsonpwrapper.com.

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

NVI
  • 14,907
  • 16
  • 65
  • 104
2

Here's another one, much faster, built on DigitalOcean & CloudFlare, utilizing caching et al: http://json2jsonp.com

fevangelou
  • 315
  • 3
  • 7
0

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
  • 794
  • 5
  • 15
  • 1
    Oh, my example is really bad. I've replaced http://elv1s.ru/x/hello.js with http://elv1s.ru/x/hi.txt – NVI Jan 26 '10 at 19:46
  • 1
    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 Jan 27 '10 at 15:07
-2

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
  • 29,161
  • 19
  • 114
  • 169
  • 1
    Get source code (http://github.com/NV/jsonpwrapper.com/) and host it yourself if you like. – NVI Jan 26 '10 at 19:58