从ajax响应中获取数据

时间:2022-10-08 21:16:03

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

在学校,我必须开发一个使用ASP.NET的Twitter客户端。

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.

在这个应用程序中,我有一个带有删除链接的tweet列表。这个链接是用helper Ajax.ActionLink()创建的,我为OnSuccess事件指定了一个回调函数。

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

这个链接没有问题:操作被执行,回调被触发,但是我不能访问Ajax响应中发送的数据。

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

回调只接收一个参数。这是该对象的转储:

>> Sys.Mvc.AjaxContext
$0: 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.

我的responseText在哪里?我知道响应有一个内容(根据Chrome开发工具),我真的很想访问它。

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

额外的好处:Ajax客户端可以自动将响应解析为JSON(该操作使用JSON方法返回JSON)吗?

Thanks ! ;)

谢谢!,)


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

这个学校项目的最后期限已经结束了。我在响应中使用了get_data。

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. :(

我对这一琐碎需求缺乏文档感到非常失望。即使现在我知道了路,我在MSDN上也找不到,太遗憾了。:(

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

回到我宝贵的Ruby on Rails上,我感觉好多了。

Good day and thanks for your help anyway ! :)

再见,谢谢你的帮助!:)

2 个解决方案

#1


2  

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.

尝试在AjaxContext上调用get_object()来获取javascript对象或get_data()来获取文本。更简单的方法是让OnSuccess函数接受一个参数,该参数将返回对象。

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" })

#2


0  

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).

如果希望在代码中访问responseText,则不希望使用AJAX(异步JavaScript和XML),而希望使用SJAX(同步JavaScript和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.

当使用AJAX时,您的代码将不会等待它将继续执行的responseText,因此如果您稍后尝试在代码中引用它,它可能不会工作,因为它可能还没有处理。

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

使用SJAX时,代码将等待responseText,然后才继续执行。

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

我不使用ASP。所以我不能帮你写代码。

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

以下是JavaScript的方法:JavaScript - AJAX / SJAX -提交表单数据到脚本。

#1


2  

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.

尝试在AjaxContext上调用get_object()来获取javascript对象或get_data()来获取文本。更简单的方法是让OnSuccess函数接受一个参数,该参数将返回对象。

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" })

#2


0  

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).

如果希望在代码中访问responseText,则不希望使用AJAX(异步JavaScript和XML),而希望使用SJAX(同步JavaScript和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.

当使用AJAX时,您的代码将不会等待它将继续执行的responseText,因此如果您稍后尝试在代码中引用它,它可能不会工作,因为它可能还没有处理。

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

使用SJAX时,代码将等待responseText,然后才继续执行。

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

我不使用ASP。所以我不能帮你写代码。

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

以下是JavaScript的方法:JavaScript - AJAX / SJAX -提交表单数据到脚本。