开发者

Get data from the ajax response

开发者 https://www.devze.com 2023-03-05 01:34 出处:网络
For school, i have to develop a Twitter client with ASP.NET. In the app, i have a list of tweets with a delete link. This link is created with the helper Ajax.ActionLink() and i specified a callback

For school, i have to develop a Twitter client with ASP.NET.

In the app, i have a list of tweets with a delete link. This link is created with the helper Ajax.ActionLink() and i specified a callback function for OnSuccess event.

This link is okay : the action is performed, the callback is triggered BUT i can't access data sent in the Ajax response.

The callback receive only one argument. Here is the dump of this object :

>> Sys.Mvc.AjaxContext
$0: 开发者_JS百科0
$1: null
$2: Sys.Net.XMLHttpExecutor
$3: Sys.Net.WebRequest
$4: null

Where is my responseText ? I know that the response has a content (according to Chrome developer tools) and i really want to access it.

Bonus : can Ajax client automatically parse the response as JSON (the action returns JSON properly with the JSON method) ?

Thanks ! ;)


The deadline of this school project is over. I used get_data on the response.

I'm quite disappointed by the lack of documentation for this trivial need. Even now i know the way, i can't find that on MSDN… Such a pity. :(

Back to my precious Ruby on Rails, i feel better.

Good day and thanks for your help anyway ! :)


Try calling get_object() on your AjaxContext to get a javascript object or get_data() to get the text. An easier method though is to have your OnSuccess function take an argument which will be the object returned.

public ActionResult ReturnJson()
{
    return Json(new { TestMessage = "Hello, world!" }, JsonRequestBehavior.AllowGet);
}

And the view...

<script type="text/javascript">
    function AjaxSuccess(obj) {
        alert(obj.TestMessage);
    }
</script>

@Ajax.ActionLink("Say Hi", "ReturnJson", new AjaxOptions() { OnSuccess = "AjaxSuccess" })


If you want to access the responseText within your code you do not want to use AJAX(Asynchronous JavaScript And XML), you want to use SJAX (Synchronous JavaScript And XML).

When using AJAX your code will not wait for the responseText it will just continue to execute so if you try referencing it later in your code it may not work as it might not have processed yet.

When using SJAX your code will wait for the responseText before continuing to execute.

I don't use ASP.NET so I can't help with the code.

Here is how it is done in JavaScript: JavaScript - AJAX / SJAX - Submit Form Data to a Script

0

精彩评论

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