M
M
Mikhail Kuligin2018-02-22 21:09:29
JavaScript
Mikhail Kuligin, 2018-02-22 21:09:29

How to parse server response after $ajax() request?

I send the request like this:

function send() {
var uri = 'http://127.0.0.1:5000/todo/api/v1.0/tasks';
var method = 'GET'
var request = {
    url: uri,
    type: method,
    contentType: "application/json",
    accepts: "application/json",
    cache: false,
    dataType: 'json',    
};    
return $.ajax(request);   
};

in the log such a mess arrives (log below), typeof says that this is an object, I can’t figure out how to extract the responseText ?
{readyState: 1, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}abort: ƒ ( statusText )always: ƒ ()complete: ƒ ()done: ƒ ()error: ƒ ()fail: ƒ ()getAllResponseHeaders: ƒ ()getResponseHeader: ƒ ( key )overrideMimeType: ƒ ( type )pipe: ƒ ( /* fnDone, fnFail, fnProgress */ )progress: ƒ ()promise: ƒ ( obj )readyState: 4responseText: "{↵    "tasks": [↵        {↵            "title": "Buy groceries",↵            "description": "Milk, Cheese, Pizza, Fruit, Tylenol",↵            "done": false,↵            "uri": "/todo/api/v1.0/tasks/1"↵        },↵        {↵            "title": "Learn Python",↵            "description": "Need to find a good Python tutorial on the web",↵            "done": false,↵            "uri": "/todo/api/v1.0/tasks/2"↵        }↵    ]↵}↵"setRequestHeader: ƒ ( name, value )state: ƒ ()status: 200statusCode: ƒ ( map )statusText: "OK"success: ƒ ()then: ƒ ( /* fnDone, fnFail, fnProgress */ )__proto__: Object

If the request is sent manually in the browser 127.0.0.1:5000/todo/api/v1.0/tasks , normal json is returned:
{
    "tasks": [
        {
            "title": "Buy groceries",
            "description": "Milk, Cheese, Pizza, Fruit, Tylenol",
            "done": false,
            "uri": "/todo/api/v1.0/tasks/1"
        },
        {
            "title": "Learn Python",
            "description": "Need to find a good Python tutorial on the web",
            "done": false,
            "uri": "/todo/api/v1.0/tasks/2"
        }
    ]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Zhivagin, 2018-02-22
@budda

Can be even easier

var uri = 'http://127.0.0.1:5000/todo/api/v1.0/tasks';
$.get(uri, function (data) {
  console.log('data from server --- ', data);
})

If you still need the option from the question - see here about .done()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question