GET Ajax returns html code in response instead of json object

前端 未结 3 1278
刺人心
刺人心 2021-01-19 00:21

I have an ajax get request as below. I am making a GET request to server.js in openshift using nodejs express. However, i get html contents in the response method instead of

相关标签:
3条回答
  • 2021-01-19 00:48

    You can use res.json to send JSON response instead of res.send

    res.json(obj)
    

    This method also set Content-Type as application/json

    0 讨论(0)
  • 2021-01-19 00:53

    res.send(names) is not JSON, You have to stringify the data using JSON.stringify.

    Try testing before the DB call to check if it works.

    res.send( JSON.stringify( {testData:'test'} ) )

    Edit

    As discussed in the comments, please check to ensure that your request is routed to the correct route that you declared.

    Does console.log("gat method"); output anything to the terminal window?

    0 讨论(0)
  • 2021-01-19 00:55

    in your $.ajax method call change this

    dataType: "text",
    

    to

    dataType: "json",
    

    refer to the documentation for the further details

    https://api.jquery.com/jQuery.ajax/

    and please check if the data you receive is a valid json, check it with http://jsonlint.com/

    0 讨论(0)
提交回复
热议问题