0

in this Question I read that in node.js you can distinguish between html requests and json requests like so:

app.get('/route', function (req, res) {
    if (req.is('json')) res.json(data);
    else if (req.is('html')) res.render('view', {});
    else ...
});

now my question is how do you make a request that is interpreted as json in node server?
cause I tried with $.ajax and $.getJson and typed in the browser and all were html requests.
this is my request

$.ajax({ type: 'GET', url: "/test", dataType:"json", success: function(data){log(data)}})
2
  • Have you set datatype = json in your ajax call? Commented Jul 4, 2015 at 11:07
  • yes I have but didn't make any difference Commented Jul 4, 2015 at 11:11

2 Answers 2

3

The req.is method checks the incoming request type by inspecting the Content-Type header therefore you need to make sure this header is set in the request before it's sent up e.g.

$.ajax({
    type: 'GET',
    url: '/route',
    contentType: "application/json; charset=utf-8",
    ....
});

However, the Content-Type header is used to determine the format of the request body, not the response. It's recommended you use the Accept header instead to inform the server of what type of format is appropriate for the response e.g.

app.get('/route', function (req, res) {
    if (req.accepts('html')) {
        res.render('view', {});
    } else if (req.accepts('json')) {
        res.json(data);
    } else {
        ...
    }
});

Then on the client, you don't need to worry about the Content-header but rather the Accept header, and jQuery already provides a handy little method for that

$.getJSON('/route', function(data) {
    ...
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks that did work, but when I enter the url in the broswer(not $.ajax) it still accepts the request as json, isn't this supposed to be html?
@user3425760 what you'll probably find is the browser is telling the server it can accept both JSON & HTML responses. You can update your get method to prioritise HTML over JSON, I've updated my answer to demonstrate this.
1

Try setting the contentType parameter

$.ajax({
    type: 'GET',
    url: 'your_url',
    data: {
        test: "test"
    },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    ....
});

EDIT:

You can use the request Module all you have to do is

var request = require('request');

var options = {
  uri: 'your_server_side_url',
  method: 'POST',
  json: {
    "data": "some_data"
  }
};

request(options, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body.id) // Print the shortened url.
  }
});

Check out that github link. May be that module will make your life easier

5 Comments

I don't need the result in the client, I check how the server is seeing the request.
first you have to make sure that the ajax request is correctly sent to the server
the request is sent to server fine, cause I can log the request.
I'm not getting any error, the request should be in type of "json" but it's always in type of "html" even with the contentType and everything.
your answer wan't what I was looking for, but you get a vote up anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.