Skip to content

How to obtain XmlHttpRequest response outside clousure. #397

@vicky5124

Description

@vicky5124
    let mut resp = String::new();
    let xhr = XmlHttpRequest::new();
    xhr.open("GET", &api_url)?;
    xhr.send()?;

    xhr.add_event_listener(|e: ReadyStateChangeEvent| {
        let target: XmlHttpRequest = e.target().unwrap().try_into().unwrap();
        if target.ready_state() == XhrReadyState::Done {
            //resp = xhr.response_text().unwrap().unwrap();
            resp = target.response_text().unwrap().unwrap().to_string();
        }
    });
    // do stuff with the respone, but only if the response is not empty

I can't use move on the clousure, otherwhise i get value borrowed after move as an error.

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