英文:
SendAsync() returns no data
问题
I made a form in Blazor WebApplication (Client). When submited, all data gets posted to a Server, where it gets processed. The server then sends a response to the client whether the operation was successful or not, but the SendAsync() method does not return any response from the server.
string json = <Serialized Object>;
var request = new HttpRequestMessage(HttpMethod.Post, "https://<server_address>");
request.Content = new StringContent(json);
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
using (var client = new HttpClient())
{
var response = await client.SendAsync(request).ConfigureAwait(false);
bool isSuccessCode = response.IsSuccessStatusCode; // false
string responseStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Console.WriteLine(responseStr); // prints empty string
if (responseStr == "Success")
return true;
else
return false;
}
The response variable does not contain any useful data, but only has default values in it (0s and nulls).
When I tried the same code(methods) with the same config (except CORS) in a console app, it worked without any problems and I got the desired response from the server.
// This worked
internal static async Task<string> Post(string url)
{
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new StringContent(data);
using var client = new HttpClient();
var result = await client.SendAsync(request).ConfigureAwait(false);
return await result.Content.ReadAsStringAsync().ConfigureAwait(false); // proper response was returned
}
Link to a GitHub project showing my issue: https://github.com/SjamesE/SendAsync-BugReport/
英文:
I made a form in Blazor WebApplication (Client). When submited, all data gets posted to a Server, where it gets processed. The server then sends a response to the client whether the operation was successful or not, but the SendAsync() method does not return any response from the server.
string json = <Serialized Object>;
var request = new HttpRequestMessage(HttpMethod.Post, "https://<server_address>");
request.Content = new StringContent(json);
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
using (var client = new HttpClient())
{
var response = await client.SendAsync(request).ConfigureAwait(false);
bool isSuccessCode = response.IsSuccessStatusCode; // false
string responseStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Console.WriteLine(responseStr); // prints empty string
if (responseStr == "Success")
return true;
else
return false;
}
The response variable does not contain any useful data, but only has default values in it (0s and nulls) 1 2
When I tried the same code(methods) with the same config (except CORS) in a console app, it worked without any problems and I got the desired response from the server.
// This worked
internal static async Task<string> Post(string url)
{
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new StringContent(data);
using var client = new HttpClient();
var result = await client.SendAsync(request).ConfigureAwait(false);
return await result.Content.ReadAsStringAsync().ConfigureAwait(false); // proper response was returned
}
Link to a GitHub project showing my issue: https://github.com/SjamesE/SendAsync-BugReport/
答案1
得分: -2
对于无意中发现这篇帖子的任何人:您不能在Blazor WebAssembly应用程序上进行无跨域请求。当使用以下代码行时,您可以发送请求,但响应不会返回到您的代码。
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
如果您仍然需要使用这些请求的响应,您需要切换到Blazor Server应用程序,或者创建一个主机并通过它路由您的请求。
英文:
For anyone stumbling upon this post: You can NOT have no-cors reqests on the Blazor WebAssembly App. When using the following line, you can send a request, but the response will not be returned to your code.
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
If you still need to use the response from those requests, you need to switch to a Blazor Server App, or to create a host and route your requests through it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论