繁体   English   中英

Objective-C在UIWebView中进行Ajax调用后获取HTML值

[英]objective-C get HTML value after ajax call in UIWebView

我想获取在UIWebView HTML button click后创建的div值。

但是问题在于,在HTML button ajax call之后一次加载了网页,而在UIWebView中没有再次加载该网页,那么如何访问在ajax调用之后生成的div值?

以下是html代码:

<a class="btn_class" params="{ &quot;container&quot;: &quot;betInfoTabReceipts&quot;, &quot;onComplete&quot;: &quot;ajaxOnCompleteTabs&quot;, &quot;onLoading&quot;: &quot;ajaxOnLoadTabs&quot;}" action="/some.something" onclick="$P('place_bet', {&quot;action&quot;: this.getAttribute('action'), &quot;params&quot;: this.getAttribute('params'), &quot;confirm&quot;: &quot;true&quot;}); return false;" id="btn_id" href="#"></a>

以下是objective-c代码:

    - (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if([webView.request.URL.absoluteString isEqualToString:@"http://myurl.com/"])
    {
//some code that executes only once
}
}

我该如何解决?

您可以在调用webview didFinishLoading委托方法时执行此javascript代码。 像这样[webView stringByEvaluatingJavaScriptFromString:@""]然后,您将获得基于Ajax的请求回调

var s_ajaxListener = new Object();
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
s_ajaxListener.callback = function () {
    window.location='mpajaxhandler://' + this.url;
};

XMLHttpRequest.prototype.open = function(a,b) {
    if (!a) var a='';
    if (!b) var b='';
    s_ajaxListener.tempOpen.apply(this, arguments);
    s_ajaxListener.method = a;
    s_ajaxListener.url = b;
    if (a.toLowerCase() == 'get') {
        s_ajaxListener.data = b.split('?');
        s_ajaxListener.data = s_ajaxListener.data[1];
    }
}

XMLHttpRequest.prototype.send = function(a,b) {
    if (!a) var a='';
    if (!b) var b='';
    s_ajaxListener.tempSend.apply(this, arguments);
    if(s_ajaxListener.method.toLowerCase() == 'post')s_ajaxListener.data = a;
    s_ajaxListener.callback();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM