因此,正如标题所说,问题是,当我发送参数时,我使用axios从Vue发送请求(我看到它使用console.log),但当我在C#(.NET Core)这些参数为空。如果有更多的信息或代码需要只是让我知道。
谢谢
Vue:
methods: {
translate() {
//This log RETURNS DATA, so parameters aren't null at this moment
console.log(this.textToTranslate, this.fromLanguage, this.toLanguage);
axios
.post(
"http://localhost:5000/api/Translate",
{
TranslateRequest: {
textToTranslate: this.textToTranslate,
fromLanguage: this.fromLanguage,
toLanguage: this.toLanguage,
},
},
{
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods":
"GET, POST, PATCH, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers":
"Origin, Content-Type, X-Auth-Token",
},
}
)
.then((res) => {
console.log(res);
this.translatedText = res.data;
})
.catch((err) => {
console.log(err.response);
alert(err.response);
});
},
},
C#:
[ApiController]
[Route("api/[controller]")]
public class TranslateController : ControllerBase
{
[EnableCors("AllowOrigin")]
[HttpGet]
public string GetTranslatedText(TranslateRequest request)
{
//request object isn't null but their parameters are
return TranslateText(request);
}
}
你的问题就在这里:
{
"TranslateRequest":{
"textToTranslate":"this.textToTranslate",
"fromLanguage":"this.fromLanguage",
"toLanguage":"this.toLanguage"
}
}
通过如下数据:
{
"textToTranslate":"this.textToTranslate",
"fromLanguage":"this.fromLanguage",
"toLanguage":"this.toLanguage"
}