代码之家  ›  专栏  ›  技术社区  ›  David Fox

如何读取通过$.ajax()返回的内容?

  •  1
  • David Fox  · 技术社区  · 14 年前

    function editaddress(id) {
            $.ajax({
                type: "POST",
                url: "/Address/Edit/" + id,
                success: function (msg) {
                    alert(msg);
                }
            });
        }
    

    是什么 msg ? 我以为它可能是一个JSON对象??当我调试时, /Address/Edit/1 退货 View(address);

    这个脚本的局部视图是一个jqueryui对话框,列出了地址,我想弹出 另一个 jqueryui对话框,用于编辑单击的记录。所以,我需要以某种方式读取返回的模型对象。我该怎么做?

    编辑:

    public ActionResult Edit(int id)
        {
            Address address = dc.Addresses.Where(x => x.AddressID == id).First();
    
            return View(address);
        }
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   Nealv    14 年前

    使用firebug,控制台。这样,您就可以看到您所做的文章、所传递的参数以及所返回的响应。

        2
  •  1
  •   Peter Ajtai    14 年前

    msg是从URL返回的数据 "/Address/Edit/" + id “/地址/编辑/”+id id and you want to pass it 身份证件 with POST, you should include data:`在jQuery中。

    1. http:// .whatever . 一旦你让它像那样工作,你可以随意删除开头,但看起来你可能缺少文件类型。
    2. data:

    data:"variable1=value1&variable2=value2 ... ", 下面是用你的代码说明的。

    $.ajax(
    {
        type: "POST",
        url: "http://www.yourdomain.com/Address/Edit.html",
        data: "id="+id,
        success: function (msg) 
        {
            alert("Data saved: " + msg);
        }
    });
    

    看看上面的例子 the jQuery.ajax() page :

    你必须把数据传进来 数据:

        3
  •  0
  •   coding_hero    14 年前