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

Extend text.get to be able to receieve request headers #38

Closed
DimitarChristoff opened this issue Feb 1, 2013 · 3 comments
Closed

Extend text.get to be able to receieve request headers #38

DimitarChristoff opened this issue Feb 1, 2013 · 3 comments
Milestone

Comments

@DimitarChristoff
Copy link
Contributor

the text plugin is being used by the json plugin (correctly or not). unfortunately, some restful servers depend on the accept header to determine the content-type of the response and may default to say, XML.

The issue is that the text plugin exposes no way to set request headers at present.

I know you can hack via config: { text: { onXhr: function(xhr){ ... } } } but the json plugin has no direct access to that. It will be much more useful to change text.get to accept an optional 4th argument headers (obj) and set these on the xhr instance before the open.

A quick hack that worked:

        text.get = function (url, callback, errback, headers) {
            var xhr = text.createXhr(), header;
            xhr.open('GET', url, true);

            if (headers && headers === Object(headers)) {
                for (header in headers) {
                    if (headers.hasOwnProperty(header)) {
                        xhr.setRequestHeader(header.toLowerCase(), headers[header]);
                    }
                }
            }
   ...

and in json.js just pass as 4th argument.

{
    Accept: 'application/json'
}

I'd do a pull request but I cannot figure if this is the best API to do it or how to mimic the same for rhino file stream.

@jrburke
Copy link
Member

jrburke commented Feb 1, 2013

This seems fine. I am open to a pull request, however, I would just use if (headers) { and skip the Object check. For the non-xhr paths, it is fine to leave them as they are, since it does not make sense in those cases.

@DimitarChristoff
Copy link
Contributor Author

cool. what of fallback to config.text.requestHeaders if no arg? good idea?
can work with CORS etc

On Friday, February 1, 2013, James Burke wrote:

This seems fine. I am open to a pull request, however, I would just use if
(headers) { and skip the Object check. For the non-xhr paths, it is fine
to leave them as they are, since it does not make sense in those cases.


Reply to this email directly or view it on GitHubhttps://github.com//issues/38#issuecomment-13007879.

Dimitar Christoff

"JavaScript is to JAVA what hamster is to ham"
@D_mitar - https://github.com/DimitarChristoff

@jrburke
Copy link
Member

jrburke commented Feb 2, 2013

I would not do anything by default, just do what it does today. CORS works "automatically" if the user tells the text plugin to use an xhr via useXhr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants