我正在尝试使用jquery发送ajax get请求,但我收到此错误:
未捕获的TypeError:非法调用 在e(jquery.min.js:4) 在dc(jquery.min.js:4) 在dc(jquery.min.js:4) 在Function.n.param(jquery.min.js:4) 在Function.ajax(jquery.min.js:4) 在home.js:2
var cid = document.getElementById("id").value;
$.ajax({
type : "GET",
url : windowLocationS + "findTourByCustomId",
data : {
id : cid
},
async : false,
cache : false,
dataType : "json",
contentType: "application/json",
success : function(jsn) {
console.log(jsn.length);
},
error : function(error) {
console.log(error);
}
});
控制器:
@RequestMapping(value= "/findTourByCustomId", method = RequestMethod.GET, headers = "Accept=application/json")
public List<classA> findTourByCustomId(@RequestParam(value = "id", required = true) String id) {
return service.findByCustomId(id);
}
如何成功获取数据?
答案 0 :(得分:0)
这对我有用:
$.ajax({
type: "GET",
url: "/Home/Index900", //I changed this url
data: {
theId: "fromCid" //changed id to theId to avoid routing issues, and simplified string from cid
},
async: false,
cache: false,
dataType: "json",
contentType: "application/json",
success: function (jsn) {
console.log(jsn.length);
},
error: function (error) {
alert("error");
console.log(error);
}
});
这是我的服务器代码,以备您需要时使用。它可能使用不同的语言:
public class HomeController : Controller
{
public ActionResult Index900(string theId)
{
return Json("AwordThatLettersWillCount", JsonRequestBehavior.AllowGet);
}
public ActionResult Index800()
{
return View();
}