dimanche 30 décembre 2018

Empty output from JSON encoding

In Firefox the http response is shown as '{}', but the alert in the client side code prints '[object object]'. I would have expected something resembling '{logoutGoogle : "true" , logoutUrl : "/_ah/logout?continue=http%3A//localhost%3A8080/"}' in the Firefox tool that show the XHR response.

I verified that the failure is the same with 'tada' as logoutUrl instead of '/_ah/logout?continue=http%3A//localhost%3A8080/'.

Code compiles and runs and I have included comments in the client and server code below to show what the results of me trying to inspect the running code with logs and with alerts are.

Client side code inside an emberjs component:

export default Component.extend({
    userUiLanguageService : service(),
    ajax : service(),
    actions: {
        logoutOnlyWebApp(){
            alert(this.get('ajax').request('/logout',{
                method : 'GET',
                data: {
                    logoutGoogle : false
                }
            }));
        },
        logoutAllOfGoogle(){
            //alert prints [object object]
            //not undefined as I would have expected
            //from the Firefox http response tool
            // that shows '{}' as the server response
            alert(this.get('ajax').request('/logout',{
                method : 'GET',
                data : {
                    logoutGoogle : true
                }
            }));
        }
    }
});

Server side go code:

type LogoutOutput struct {
    logoutSuccessful string
    logoutUrl string
}

func logout(w http.ResponseWriter, r *http.Request){
    c := appengine.NewContext(r)
    u := user.Current(c)

    if u == nil {
        http.Redirect(w, r, "/", http.StatusSeeOther)
        return
    }

    logoutGoogle:=r.URL.Query().Get("logoutGoogle")
    //below prints 'true'
    log.Errorf(c,"%v",logoutGoogle)
    logoutGoogleBool:=false
    if logoutGoogle == "true" {
        logoutGoogleBool=true
    } else if logoutGoogle == "false" {
        logoutGoogleBool=false
    } else {
        log.Errorf(c,"Value is not true or false",logoutGoogle)
    }

    url := ""
    var err error
    if logoutGoogleBool { // LOGOUT ALL OF GOOGLE
        url, err = user.LogoutURL(c, "/")
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
    }

    //below prints '/_ah/logout?continue=http%3A//localhost%3A8080/'
    log.Errorf(c,"%v",url)
    logout_output:=LogoutOutput{logoutSuccessful:"true",logoutUrl:url}
    logout_output_json,err := json.Marshal(logout_output)
    //below prints '[123 125]'
    log.Errorf(c,"%v",logout_output_json)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    //w.Header().Set("Location", url)
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusOK)
    w.Write(logout_output_json)
    //fmt.Fprintf(w,logout_output_json)
}




Aucun commentaire:

Enregistrer un commentaire