Skip to content
This repository was archived by the owner on Jan 19, 2018. It is now read-only.
This repository was archived by the owner on Jan 19, 2018. It is now read-only.

How to access previous Ajax response in a chain of ajax requests #8

@fbuchinger

Description

@fbuchinger

Hi,

I'm writing a javascript client for a webservice that requires a lot of inter-dependent requests (i.e. you need the parts of the response from request1 as GET parameters in request 2). To avoid the extensive use of nested callbacks, I like to develop the client using jquery/jsdeferred's next statement. To make my code even shorter, I like to register my custom request/response handler as Deferred shorthand function. Here is what I have so far:

Deferred.define();

function makeRequest (method, params){
    $.extend(params,{method: method, api_key: 123});
     var url = $.param('http://api.example.com', params);
    return next($.get(url))
}

Deferred.define(makeRequest, ["next","parallel"]);


makeRequest('getIdForUsername', {username:'dummy'}).
//user_id is from the response of 'getIfForUsername' request
makeRequest('getUserStatus', {user_id:user_id}).
next(function(userstatus){
    //generate HTML for user status
});

My questions:

  • how can I "pass on" the user_id obtained in the first request to parameters of the second request?
  • How can I differentiate between parallell and sequential execution of the makeRequest- function?
  • How can I implement error handling proberly? (e.g. request1 gives an error response --> chain should stop and user should be notified of bad response)

Thanks for developing jsdeferred!

Franz

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions