gpt4 book ai didi

javascript - 如何从 Controller 获取字符串并在 View 中使用它?

转载 作者:行者123 更新时间:2023-11-30 14:02:37 25 4
gpt4 key购买 nike

我正在尝试从我的 Controller 获取一些字符串并在 View 中使用它。ActionResult 的工作原理如下:

public ActionResult GetSymbols()
{
string result = "SVG-String";
return Content(result);
}

这个结果将是一个 svg 格式的字符串,应该在我的 svg 绘图中显示在最后。我尝试使用 JavaScript 调用此 Controller 操作,我可以访问 Controller ,但如何使用字符串?我看不到任何结果,那么将返回的字符串放入变量的正确方法是什么?

最后的尝试是这样的:

$(document).ready(function () {
$.ajax({
url: "/Symbols/GetSymbols/",
method: "GET",
async: false,
data: "",
dataType: "string",
success: function (data) { alert(data); }
});
});

最佳答案

这个怎么样,

Controller 返回类型更改为 Json

[HttpGet]
public JsonResult GetSymbols()
{
string result = "SVG-String";
return Json(result, JsonRequestBehavior.AllowGet);
}

你的 javascript 就像,

$(document).ready(function () {
$.ajax({
method: "GET",
url: "/Symbols/GetSymbols/",
async: false,
dataType: "json",
success: function (data) {
alert(data);
},
error: function (response) {
console.log(response);
}
});
});

关于javascript - 如何从 Controller 获取字符串并在 View 中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56055784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com