gpt4 book ai didi

javascript - 服务器响应 404。ajax 调用未到达 Controller

转载 作者:行者123 更新时间:2023-12-03 05:12:00 25 4
gpt4 key购买 nike

我正在编写一个应用程序,它从文本框中获取字符串并在单击按钮时调用服务器方法。但是我收到错误消息: http://localhost:33655/Search/Search1 404(未找到)检查后:我看到错误。无法加载资源。服务器响应 404。

这是我的 js 和 Controller 代码。

function look_up_term() {
var search = {};
var Query = document.getElementById("SearchString").value;

$.ajax({
type: "POST",
url: '/Search/Search1',
contentType: "application/json; charset=utf-8",
data: Query,
dataType: "json",
success: function (response) {
alert("success");
},
error: function (response) {
alert("error");
}
});
}

Controller 代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{

public class SearchController : Controller
{
[HttpGet]
public JsonResult Search1(String sLookupIds)
{
return Json(new JsonResult()
{
Data = "Result"
}, JsonRequestBehavior.AllowGet);
}

}
}

有什么想法吗?

最佳答案

首先将 [HttpGet] 替换为 [HttpPost]。

[HttpPost]
public JsonResult Search1([FromBody]String sLookupIds)
{
...
}

查询变量在 ajax 调用中保存什么?尝试发送类似

的数据
$.ajax({
type: "POST",
url: '/Search/Search1',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(Query),
dataType: 'json',
success: function (response) {
alert("success");
},
error: function (response) {
alert("error");
}
});

关于javascript - 服务器响应 404。ajax 调用未到达 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41772004/

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