Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to obtain XmlHttpRequest response outside clousure. #397

Open
vicky5124 opened this issue Mar 25, 2020 · 1 comment
Open

How to obtain XmlHttpRequest response outside clousure. #397

vicky5124 opened this issue Mar 25, 2020 · 1 comment

Comments

@vicky5124
Copy link

    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.

@izissise
Copy link

izissise commented Apr 1, 2020

You can do something like this:

 xhr.add_event_listener(move |e: ReadyStateChangeEvent| {
            // Magically retrieve the request object through js and std::web
            let xhr = Reference::from(e.target().unwrap()).downcast::<XmlHttpRequest>().unwrap();
            match xhr.ready_state() {
                XhrReadyState::Done => { 
...

But you won't be able to retrieve the headers easily until #381 is merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants