开发者

Online JSONP converter/wrapper

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.


I built jsonpwrapper.com.

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


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜