开发者

How do I read what is returned via $.ajax()?

I don't receive a开发者_运维问答n alert even though this successfully returns the model I'm requesting?

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

What is msg? I thought it was maybe a JSON object?? When I debug, /Address/Edit/1 returns View(address); but how can I read that object in my view? Do I need to make some other post?

The partial view with this script is a jQuery UI Dialog listing Addresses, and I want to popup another jQuery UI Dialog on top of it to edit the record clicked. So, I need to somehow read the returned model object. How do I do this?

Edit:

public ActionResult Edit(int id)
    {
        Address address = dc.Addresses.Where(x => x.AddressID == id).First();

        return View(address);
    }


Use firebug, console. That way yous can see the post you did, see the parameters you passed, and see the response that is returned.

The response will be your "msg" variable


msg is the data returned from the URL "/Address/Edit/" + id in POST format. It doesn't look like you are passing any data to the URL of "/Address/Edit/" + id. If the data is contained in id and you want to include it in the URL, you should use GET. If the URL is '"/Address/Edit.html"and you want to pass itidwith POST, you should includedata:` in your jQuery.

  1. I suggest you initially type out the whole URL just to make your life easier. Include it from http:// to .whatever. Once you get it working like that, you can play around with removing the beginning, but it looks like you might be missing the file type.
  2. You have to format your data and include it with data:

To pass data with post and jQuery you use the format data:"variable1=value1&variable2=value2 ... ", Below it's illustrated using your code.

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

Take a look at the examples on the jQuery.ajax() page:

You have to pass the data in data:, you can't pass the data in the URL if you use POST, you can only do so with GET.


It also helps a lot to use Firebug with Firefox, and use the console. It will show the return AJAX call and the information it contains.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜