Rop*_*tah 1 asp.net-mvc javascript-events
是否有可能获得对onSuccess函数中调用Ajax请求的triggerElement的引用?
<%=Ajax.ActionLink("x", a, r, New AjaxOptions With {.OnSuccess = _
"function(context) {alert('get triggerElement reference here?');}" })%>
Run Code Online (Sandbox Code Playgroud)
该页面呈现为:
<a href="/<whatever>/<action>"onclick =" Sys.Mvc.AsyncHyperlink.handleClick(this,new Sys.UI.DomEvent(event),{insertionMode:Sys.Mvc. InsertionMode.replace,onSuccess:Function.createDelegate(this,function(context){alert('get triggerElement reference here?');})});"> x </a>
所以我们来看看Sys.Mvc.AsyncHyperlink.handleClick里面Scripts\MicrosoftMvcAjax.debug.js:
Sys.Mvc.AsyncHyperlink.handleClick = function Sys_Mvc_AsyncHyperlink$handleClick(anchor, evt, ajaxOptions) {
/// omitted doc comments
evt.preventDefault();
Sys.Mvc.MvcHelpers._asyncRequest(anchor.href, 'post', '', anchor, ajaxOptions);
}
Run Code Online (Sandbox Code Playgroud)
因此ActionLink的呈现到锚定("a")的标签,与"点击"事件,它使用Sys.Mvc.AsyncHyperlink.handleClick与此作为参数之一,映射到锚定.
然后是锚点作为第四个参数的这个Sys.Mvc.MvcHelpers._asyncRequest调用.我们来看看:Sys.Mvc.MvcHelpers._asyncRequest
Sys.Mvc.MvcHelpers._asyncRequest = function Sys_Mvc_MvcHelpers$_asyncRequest(url, verb, body, triggerElement, ajaxOptions) {
/// omitted documentation
if (ajaxOptions.confirm) {
if (!confirm(ajaxOptions.confirm)) {
return;
}
}
if (ajaxOptions.url) {
url = ajaxOptions.url;
}
if (ajaxOptions.httpMethod) {
verb = ajaxOptions.httpMethod;
}
if (body.length > 0 && !body.endsWith('&')) {
body += '&';
}
body += 'X-Requested-With=XMLHttpRequest';
var requestBody = '';
if (verb.toUpperCase() === 'GET' || verb.toUpperCase() === 'DELETE') {
if (url.indexOf('?') > -1) {
if (!url.endsWith('&')) {
url += '&';
}
url += body;
}
else {
url += '?';
url += body;
}
}
else {
requestBody = body;
}
var request = new Sys.Net.WebRequest();
request.set_url(url);
request.set_httpVerb(verb);
request.set_body(requestBody);
if (verb.toUpperCase() === 'PUT') {
request.get_headers()['Content-Type'] = 'application/x-www-form-urlencoded;';
}
request.get_headers()['X-Requested-With'] = 'XMLHttpRequest';
var updateElement = null;
if (ajaxOptions.updateTargetId) {
updateElement = $get(ajaxOptions.updateTargetId);
}
var loadingElement = null;
if (ajaxOptions.loadingElementId) {
loadingElement = $get(ajaxOptions.loadingElementId);
}
var ajaxContext = new Sys.Mvc.AjaxContext(request, updateElement, loadingElement, ajaxOptions.insertionMode);
var continueRequest = true;
if (ajaxOptions.onBegin) {
continueRequest = ajaxOptions.onBegin(ajaxContext) !== false;
}
if (loadingElement) {
Sys.UI.DomElement.setVisible(ajaxContext.get_loadingElement(), true);
}
if (continueRequest) {
request.add_completed(Function.createDelegate(null, function(executor) {
Sys.Mvc.MvcHelpers._onComplete(request, ajaxOptions, ajaxContext);
}));
request.invoke();
}
}
Run Code Online (Sandbox Code Playgroud)
所以原始锚点现在是triggerElement,但正如你所看到的,这个参数从未在函数体中使用过.
所以,如果你想对triggerElement有某种"正式"(或记录)的引用 - 没有这样的事情.
但是,嘿,这是JavaScript,所以只要浏览器没有移动到另一个页面,包括调用堆栈,你几乎可以访问任何东西.例如:
<script type="text/javascript">
function a(p, q)
{
b();
}
function b() {
var x = arguments.caller[1];
alert(x); // boo!
}
a(789, "boo!");
</script>
Run Code Online (Sandbox Code Playgroud)
所以最终你可以破解它并访问原始锚点.我建议你做以下事情:
OnBegin.triggerElement,并将其作为属性添加到原始文件中
ajaxOptions(也可以访问)OnSuccess函数中,访问您的黑客属性
ajaxOptions.| 归档时间: |
|
| 查看次数: |
2132 次 |
| 最近记录: |