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

Http.SendJsonAsync failed on 0.7 #1731

Closed
auc31 opened this Issue Nov 27, 2018 · 6 comments

Comments

Projects
None yet
4 participants
@auc31

auc31 commented Nov 27, 2018

Hi

we try to follow this tutiorial

https://ankitsharmablogs.com/asp-net-core-crud-using-blazor-and-entity-framework-core/

for CRUD operation on V0.3 of blazor Work perfect

but on our 0.7 version when we try to send data with

await Http.SendJsonAsync(HttpMethod.Post, "/api/Employee/Create", emp);

the data not send to api method create and the nav refresh the blazor app :( (stay on creation page)

thanks for your help

@danroth27

This comment has been minimized.

Member

danroth27 commented Nov 28, 2018

Do you see the request in the network tab of the browser dev tools? Is there an error response from the server? Or are there any errors in the console window? Could you please also check the logs on the server to see if there are any errors?

@enetstudio

This comment has been minimized.

enetstudio commented Nov 28, 2018

@auc31,
This is the culprit:

<button type="submit" class="btn btn-default" onclick="@(async () => await CreateEmployee())">Save</button>

The type attribute should be set to button: type="button"
This is also the culprit: <form></form>

I can't help it if you know what I mean..just don't ...

@auc31

This comment has been minimized.

auc31 commented Nov 28, 2018

hi

thanks for your answer, with the type "button" we have this error :

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST http://localhost:57845/api/SampleData/Create application/json; charset=utf-8 72
'dotnet.exe' (CoreCLR: clrhost) : Chargé 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.6\System.Diagnostics.StackTrace.dll'. Chargement des symboles ignoré. Le module est optimisé et l'option du débogueur 'Uniquement mon code' est activée.
'dotnet.exe' (CoreCLR: clrhost) : Chargé 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.6\System.Reflection.Metadata.dll'. Chargement des symboles ignoré. Le module est optimisé et l'option du débogueur 'Uniquement mon code' est activée.
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware:Error: An unhandled exception has occurred while executing the request.

System.InvalidOperationException: The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request.

at Microsoft.AspNetCore.SpaServices.SpaDefaultPageMiddleware.<>c__DisplayClass0_0.b__1(HttpContext context, Func`1 next)
at Microsoft.AspNetCore.Builder.UseExtensions.<>c__DisplayClass0_1.b__1(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 63.5363ms 500 text/html; charset=utf-8
Microsoft.AspNetCore.Server.Kestrel:Information: Connection id "0HLIKVFJ24QQ0", Request id "0HLIKVFJ24QQ0:00000008": the application completed without reading the entire request body.

@enetstudio

This comment has been minimized.

enetstudio commented Nov 28, 2018

"thanks for your answer, with the type "button" we have this error :"

Mate, don't you understand what's wrong with the code...?

You should remove the <form> tag
You should also replace the string "submit" with the string "button" in the type attribute. It should be like that:

 <button type="button" class="btn btn-default" onclick="@(async () => await CreateEmployee())">Save</button>

instead of

 <button type="submit" class="btn btn-default" onclick="@(async () => await CreateEmployee())">Save</button>

When you use type="submit", the SendJsonAsync is not called, the form is submitted to the server. This is basic... When you use type="button"; this allow JavaScript to execute, perform Ajax call (fetch API)to the server, and add your lousy employee data, say to a database.

I don't want to speak ill of others, but you should not visit that web site. You are not the first person who have faced such unnecessary issues just because the author of the tutorial himself is lacking any basic knowledge of what he's teaching...
Learn Blazor here: http://learn-blazor.com/getting-started/what-is-blazor/
But start with HTML and some JavaScript

Hope this helps...

@SteveSandersonMS

This comment has been minimized.

Member

SteveSandersonMS commented Nov 28, 2018

Thanks for answering, @enetstudio!

@auc31

This comment has been minimized.

auc31 commented Nov 28, 2018

Many thanks for your answer @enetstudio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment