박성훈
박성훈

Reputation: 1

Execute the java script within the responsed html in blazor webassembly

As of now, I implemented the custom uri scheme logic within the blazor webassembly/aspnetcore environment. The problem occurred in the following situation.

  1. click the specific button in blazor component, the backend Api server will respond to this with the custom uri scheme.
  2. execute the embeded javascript function without user action.
  3. error occurred while executing the java script.

I couldn't find any relevant solution with this.

[server response]

<!doctype html>
<html lang=\"en\">
 <head>
   <script src=\"/res/vender/jquery/jquery-3.5.1.min.js\"></script>
   <script type=\"text/javascript\">
     $(document).ready(function() {
       RunRestBuilder(
     }
   </script>
     <script language=\"JavaScript\" type=\"text/javascript\">
         function RunRestBuilder()         
  {location.href="APIRunner://runmode:0;username:pink;password:xxx;project:myproject;id: 
  {1};testcase:{test};testtype:{1};";
         }
    </script>
 </head>
 <body>
   <a href="#"onclick=\"RunRestBuilder(\">Execute</a>
 </body>
</html>

[blazor service]

public async Task<string> Edit(string id, string testCase, string testType)
        {
            var queryStringParam = new Dictionary<string, string>
            {
                ["projectName"] = await _localStorage.GetItemAsync<string> 
        ("projectName"),
                ["id"] = id,
                ["testCase"] = testCase ?? string.Empty,
                ["testType"] = testType};
            using (var response = await 
      _client.GetAsync(QueryHelpers.AddQueryString("restbuilder/edit", 
      queryStringParam)))
            {
                if (!response.IsSuccessStatusCode)
                {
                    var error = await response.Content.ReadAsStringAsync();
                    _snackbar.Add($"error occured. errorMessage:{error}", 
       Severity.Error);
                    return string.Empty;
                }

                var result = await response.Content.ReadAsStringAsync();

                return result;
            }
        }

[blazor component]

await RestBuilderService.Edit(id, testCase, testType);
await Js.InvokeVoidAsync("RunRestBuilder");

[Error Message] 'Could not find 'RunRestBuilder' ('RunRestBuilder' was undefined).Error: Could not find 'RestBuilder' ('RestBuilder' was undefined). at http://localhost:5002/_framework/blazor.webassembly.js:1:1287 at Array.forEach ()\n at e.findFunction (http://localhost:5002/_framework/blazor.webassembly.js:1:1247) at b (http://localhost:5002/_framework/blazor.webassembly.js:1:2989) at http://localhost:5002/_framework/blazor.webassembly.js:1:3935 at new Promise () at Object.beginInvokeJSFromDotNet (http://localhost:5002/_framework/blazor.webassembly.js:1:3908) at Object.w [as invokeJSFromDotNet] (http://localhost:5002/_framework/blazor.webassembly.js:1:64232) at _mono_wasm_invoke_js_blazor (http://localhost:5002/_framework/dotnet.5.0.13.js:1:190800) at do_icall (wasm://wasm/00aba242:wasm-function[10596]:0x194e4e)'

Upvotes: 0

Views: 425

Answers (1)

Surinder Singh
Surinder Singh

Reputation: 1458

Your JavaScript function name is

function RunRestBuilder()

And you are wrongly calling it

await Js.InvokeVoidAsync("RestBuilder");

So it should be

await Js.InvokeVoidAsync("RunRestBuilder");

    

Upvotes: 0

Related Questions