Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Actually I had called the script function from c# but without complete script function invokeasync return value but i need return value After complete js function excution

JavaScript
function ajax() {
    var isbool = false;
    let xhr = new XMLHttpRequest();     xhr.onload = function () {
        if (xhr.status == 200 && xhr.status < 300) {
            isbool = true;
        }
    }
    xhr.open('GET', "https://jsonplaceholder.typicode.com/posts", true);
    xhr.send();     

   return isbool;
 }


I had called this function from c#
C#
var isonline = await JSRuntime.InvokeAsync<bool>("ajax");



Isonline return false because invokeAsync return false before complete the Onload event.

I need return value after complete js function

please help me.
Thanks

What I have tried:

Isonline return false because invokeAsync return false before complete the Onload event.

I need return value after complete js function 
Posted
Comments
Graeme_Grant 2-Feb-23 11:41am    
Blazor WASM/Web Assembly or Blazor Server?
Ramesh p 2-Feb-23 12:08pm    
Blazor WASM/
Ramesh p 3-Feb-23 1:41am    
please reply
Graeme_Grant 3-Feb-23 21:00pm    
You replied to yourself, so I did not get pinged.

Why not use HttpClient in your blazor code, why use javascript?

To answer your question, the call is async, so you need code the javascript accordingly. But again, why do this in javascript when there is a perfectly good version in C#...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900