Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONP doesn't seem to be working #822

Closed
muoto opened this issue May 6, 2013 · 12 comments · Fixed by #823
Closed

JSONP doesn't seem to be working #822

muoto opened this issue May 6, 2013 · 12 comments · Fixed by #823
Assignees
Labels
bug Bug or defect
Milestone

Comments

@muoto
Copy link

muoto commented May 6, 2013

Does hapi support JSONP? The following doesn't work:

http://localhost:8000/hello?jsonp=mycallback

Does someone have an example of how to use hapi with JSONP?

@muoto
Copy link
Author

muoto commented May 6, 2013

#531 suggests that JSONP support is in hapi, but can't seem to get it to work.

@hueniverse
Copy link
Contributor

Full example:

var Hapi = require('hapi');
var server = new Hapi.Server();

var handler = function () {

    this.reply({ some: 'value' });
};

server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } });

server.inject('/?callback=me', function (res) {

    // res.payload is 'me({"some":"value"});'
});

hueniverse pushed a commit that referenced this issue May 6, 2013
@muoto
Copy link
Author

muoto commented May 6, 2013

There seems to be a BUG. I have it setup as such:

var getUser = {
    handler: function (request) {
var parts = this.params.name.split('/');
                request.reply({  first: parts[0], last: parts[1]  });
    },
    jsonp: 'callback'
};

server.route({
    method: 'GET',
    path: '/user/{name*2}',
    config: getUser,
});

THEN I try:
http://localhost:8000/user/john/doe?callback=docall

The EXPECTED result is:
docall({"first": "john","last": "doe"});

The ACTUAL result is:
docall

@hueniverse hueniverse reopened this May 6, 2013
@hueniverse
Copy link
Contributor

I just tried this test:

            var handler = function () {

                var parts = this.params.name.split('/');
                this.reply({ first: parts[0], last: parts[1] });
            };

            var server = new Hapi.Server(0);
            server.route({
                method: 'GET',
                path: '/user/{name*2}',
                config: {
                    handler: handler,
                    jsonp: 'callback'
                }
            });

            server.start(function () {

                Request(server.info.uri + '/user/1/2?callback=docall', function (err, res, body) {

                    expect(err).to.not.exist;
                    expect(body).to.equal('docall({"first":"1","last":"2"});');
                    expect(res.headers['content-type']).to.equal('text/javascript; charset=utf-8');
                    done();
                });
            });

And it works fine. Can you change it to reproduce the issue?

hueniverse pushed a commit that referenced this issue May 6, 2013
@muoto
Copy link
Author

muoto commented May 6, 2013

Can you try the URL in your browser and confirm that it works that way?

@muoto
Copy link
Author

muoto commented May 6, 2013

Checked your example and it works fine in node.js, but when you try and use the URL in HTML from your brower as such:
..
< script src="http://localhost:8000/user/1/2?callback=docall" >< /script >
..
Then it DOES NOT work. It returns only "docall", the rest of the JSON is missing!

@hueniverse
Copy link
Contributor

I see it. Debugging.

@hueniverse hueniverse mentioned this issue May 6, 2013
@ghost ghost assigned hueniverse May 6, 2013
@hueniverse
Copy link
Contributor

@muoto Can you try the branch?

@muoto
Copy link
Author

muoto commented May 6, 2013

@hueniverse - fixed! Thank you!

@hueniverse
Copy link
Contributor

It was harder to write the test than to fix the bug...

jmonster pushed a commit to jmonster/hapi that referenced this issue Feb 10, 2014
jmonster pushed a commit to jmonster/hapi that referenced this issue Feb 10, 2014
jmonster pushed a commit to jmonster/hapi that referenced this issue Feb 10, 2014
@hueniverse
Copy link
Contributor

Just look at the tests.

@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants