【问题标题】:JQuery Ajax Get TypeError: Illegal invocationJQuery Ajax 获取 TypeError:非法调用
【发布时间】:2017-05-31 13:36:45
【问题描述】:

我正在尝试使用 jquery 发送一个 ajax 获取请求,但我收到此错误:

未捕获的类型错误:非法调用 在 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);
    }

如何才能成功获取数据?

【问题讨论】:

  • 您在问题中放置的 js 代码 sn-p 是您的 home.js 文件吗?
  • @eeya 是的,sn-p 代码在home.js
  • 如果可以,可以更新您的问题并向我们展示您如何包含您称为jquery.jshome.js&lt;script&gt; 标签?

标签: jquery ajax spring


【解决方案1】:

这对我有用:

$.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();
    }

【讨论】:

    猜你喜欢
    • 2019-09-21
    • 2017-11-03
    • 2018-12-02
    • 2014-08-23
    • 2014-04-10
    • 2015-08-13
    • 2015-02-13
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多