Closed (duplicate)
Project:
Views Datasource
Version:
6.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Reporter:
Created:
16 Oct 2009 at 12:18 UTC
Updated:
9 Sep 2013 at 16:10 UTC
Jump to comment: Most recent file
Comments
Comment #1
abritez commentedI am having issues with this myself. Have you gotten it to work on your end? Not sure if i did something wrong on my end, but the JSON/P patch didn't work for me.
Comment #2
valkum commentedHere are my JSONP changes:
works on this way:
http://example.com/path_to_view?jsonp=callback_name
Comment #3
allisterbeharry commentedThanks so much for the patch. I'll commit it and enable the JSONP format in the next release which should be out today(26th).
Comment #4
valkum commentedi think it is better to name the jsonp variable callback and add jsonp as a true/false variable to standart json output. so if you call http://exmaple.com/test?jsonp=true&callback=test it return test(data)
the user can activate jsonp support for normal json extra.
In my code you shuld change the contenttyp i dont now if you have it done in dev release. ;)
i have write some code for dev. i will post it later. with secure callback function.
edit:
Description:
you call the site with params jsonp and callback if jsonp is true you get jsonp output. if callback contains a-zA-Z0-9 you get the output else you get an 404 error. because of javascript hacking.
Comment #5
valkum commenteddo you put this into the module ?
Comment #6
allisterbeharry commentedSorry, I haven't added it as yet - it will be in the beta1 release.
Comment #7
GloryFish commentedHas the patch been tested with 6.x-1.x-dev ?
I applied the patch from #4 to our install of 6.x-1.x-dev. I checked "Allow use of JSONP" and hit the path using jsonp=true&callback=mycallback
I get JSON data back from that, but it isn't wrapped in the callback function. Upon closer inspection it appears that the views_json_render() function in view-view-json.tpl.php file is never called. Am I missing an option or is there an issue with the patch. I'm guessing the former is more likely, but I wanted to be sure.
I've tested with the following options:
- Field output: both normal and raw
- Plaintext output: checked
- JSON Data Format: Simple
- Content type: both "application/json" and "text/json"
- Views API mode: unchecked
- Allow use of JSONP: checked
Comment #8
kostajh commentedsubscribing
Comment #9
jarchowk commented#4 patch Worked for me just fine
Comment #10
cap60552 commented#4 patch would not apply to the current 6.x-1.x-dev build due to the fundamental changes in the dev version.
I have created a new patch that takes these new designs into account, and seems to work for me. I have attached the patch, and the one additional theme file needed to implement JSONP simple.
The gzip / tarball contents should be placed in the 'views_datasource/theme' directory. To use JSONP, select "Simple JSONP Callback" as the JSON Data format under the style settings in your view. Then pass the URL of your view the standard callback= parameter in your javascript and you should receive a valid jsonp result.
Eg: http://www.example.com/path/to/my/view?callback=
Comment #11
allisterbeharry commentedThanks valkum and cap60552 for the code, sorry I've taken so long to include this. I just took your idea and implemented in in the latest views_json module. There is now an option called JSONP Prefix; if you specify a text string here, the JSON output will be enclosed in parentheses and prefixed with the text string. This code is committed to CVS and will be in the beta2 release
Comment #12
allisterbeharry commentedAdded JSON support in beta2: http://drupal.org/node/855894
Comment #14
cap60552 commentedYour solution doesn't seem to allow for the passing of the prefix via "callback=" rather it only allows for a predesignated function name to be appended to the json at the time the view is setup. In JSONP the function name is an anonymous function who's name is generated at runtime, so the function name (the prefix) changes with each call and cannot be specified in advance.
Comment #15
arthur5005 commentedYes, imho this is not fixed. It's safer, more flexible and better implemented if you don't have to define a globally accessible function ahead of time. In my case I have an embedded webapp which checks for updates on our company website. I've had to include a global callback in my javascript in order to handle the JSONP reply, which is causing all kinds of ugliness. I would be for allowing the current functionality and also optionally allowing for a dynamically generated callback through a parameter 'callback='.
This is how jQuery expects JSONP to be implemented, and as such it's how this module should implement it as well. I'll have a bit of free time soon, I'll look into proposing a patch.
Comment #16
mcantelon commentedIf you drop the file linked to below in your theme directory (naming it 'views-views-json-style-simple.tpl.php') then go to your modules page and click 'Update' you'll be good to go:
http://gist.github.com/raw/578650/dcf4660d08a3458373d7754cd1f63de185dfa4...
(It makes the JSONP work like it should, playing nice with Jquery and the like.)
Comment #17
ianchan commentedHow do I format the URL to retrieve the JSONP output? IF I use http://mydomain.org/feed/?callback=, I get an error in Firefox saying that my prefix is undefined. Could someone provide an example of how to make this work? Thanks!
Comment #18
jlporter commentedallowing jsonp to any json view with a url argument will be a big security issue. It should only be configured by the user with administer views permission. Otherwise any site could pull your views/json data and use it as their own.
Comment #19
cap60552 commentedianchan, using jquery the following example snipit works for me:
The second ? in jquery is the key to solving your undefined prefix problem. This causes jquery to generate a random callback function name such as "AdEgS00421" that is passed along in place of the second ? in the getJSON functions URL string.
The resulting output from the server would be something such as:
Comment #20
cap60552 commentedjlporter
The whole point of jsonp is to allow code on domain a, to access data from domain b. This is only a problem if the JSON-P encoded data contains sensitive information. In my case, I am using it to allow information about our library, such as the library's hours, or the status of our online chat with a librarian system to be embedded into other sites.
This is very useful in the case of creating widgets that are embedded into research databases provided by 3rd party vendors.
Comment #21
liminu commentedNot works with current alpha, can you give me a module that woks with jsonp?
Comment #22
bc commentedmcantelon + others who need a jquery-style callback GET value,
The template in that gist works well, but for security you should scrub the $_GET input like so:
Comment #23
SilviaT commentedHow about the D7 version? Is the jsonp been handled? I'm having issues too.
Comment #24
bc commentedYou can use the same code described above, but you'll need to change drupal_set_header to drupal_set_http_header
Comment #25
bc commentedupdated gist, rolls together thread tidbits: https://gist.github.com/1795328
Comment #26
dankh commentedDoesn't work with Dojo JSONP. Dojo callbacks are in the form :
dojo.io.script.jsonp_dojoIoScript1._jsonpCallbackAFAIK the regular expression \W is "any non-word character" which is too restrictive for Dojo. The wanted regular expression is the one that matches the name of a single JavaScript function.
I have researched what are the characters allowed in a JavaScript function name, but there is a lot of stuff allowed (http://www.ecma-international.org/publications/standards/Ecma-262.htm). So I just wrote something more permissive :
Note that I have removed the negation. This basically allows dot in function name.
Comment #27
Ecio commentedAbout the gist linked in #25:
the commented Drupal7 change to drupal_http_header is not right, i had to modify it in this way:
in order to use the right D7 function and parameter passing
Comment #28
bc commentednice catch, changed the gist :) drupal_function_names_from_memory()
Comment #29
yannickooFound a patch in another issue (#2010558: Option to add JSONP Prefix according to the callback query string parameter) and it's working fine.
Comment #30
yannickooI close this issue because #1673488: JSONP has no dynamic callback was fixed :)
Comment #31
yannickooDuplicate makes more sense...