![](/img/trans.png)
[英]Uncaught RangeError: Maximum call stack size exceeded in jquery ajax call
[英]ASP.NET/WebAPI: $.ajax has 404 and $.getJSON has Uncaught RangeError: Maximum call stack size exceeded
我正在使用ASP.NET Web API和一個C#項目,嘗試從JavaScript進行調用。
這是我的JavaScript:
function LoadGraph() {
var file = document.getElementById("file-datas");
if ('files' in file)
{
if (file.files.length == 0) {
Console.log("Empty file");
} else {
var text = file.files;
/*$.ajax({
url: "api/Graph",
type: "POST",
dataType: "json",
processData: false,
contentType: false,
data: $(text).serialize(),
cache: false,
success: function (data) {
console.log(data);
}
})*/
//$.getJSON('api/Graph', file, function (data) { console.log(data) });
}
}
這是我的控制器:
[Authorize]
[RoutePrefix("api/Graph")]
public class GraphController : ApiController
{
// POST: api/Graph
[HttpPost]
[Route("All", Name = "LoadGraph")]
public IHttpActionResult LoadGraph(string text)
{
RecursiveGraph result = test.SecondMethodFruchterman(text);
return Ok(result.name);
}
}
如您所見,我在JavaScript中嘗試了兩種方法:
POST http://localhost:53497/Home/api/Graph 404 (Not Found)
。 Uncaught RangeError: Maximum call stack size exceeded
。 result.name是一個字符串,但是我想在解決這些問題並得到可行的結果后自行傳送結果。
第一個問題是解析的URL是POST http://localhost:53497/Home/api/Graph 404 (Not Found)
,這是錯誤的,因為您的URL實際上應該是http://localhost:53497/api/Graph
。 要解決此問題,請在AJAX網址前添加/
。
第二個問題可能是服務器錯誤以文本形式返回到Web客戶端。 這可能是由於您的遞歸永遠不會終止。 我看不到該代碼的詳細信息,但是RecursiveGraph result
提示了我。 我敢打賭,您的遞歸方法永遠不會終止,這肯定會使調用堆棧最大。
我認為您應該將ajax
調用更改為:
$.ajax({
url: '@Url.Action("LoadGraph", "Graph")',
type: "POST",
traditional: true,
data: {
text: $(text).serialize()
},
cache: false,
success: function (data) {
console.log(data);
}
})
您得到404(未找到錯誤代碼),因為您的ajax發布網址api/Graph
只是您引用的控制器
為什么甚至將控制器RoutePrefix更改為“ api / Graph”? 這樣做有特殊原因嗎?
這是更正的代碼
//[Authorize]
[RoutePrefix("api/Graph")]
public class GraphController : ApiController
{
// POST: api/Graph
[Route("LoadGraph", Name = "LoadGraph")]
public IHttpActionResult LoadGraph([FromBody] string text)
{
RecursiveGraph result = test.SecondMethodFruchterman(text);
return Ok(result);
}
}
我需要添加[FromBody],因為字符串不是模型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.