javascript - 使用此(或其他 javascript)函数了解 XMLHttpRequest 响应?

标签 javascript ios iphone uiwebview xmlhttprequest

有没有办法让 iphone/iPad 访问 XMLHTTPRequests,即来自 UIWebView 中网页的 ajax 响应。我已经尝试了几个解决方案,其中更改了响应 url,以便应用程序可以识别它确实是一个 ajax 响应。

我是 ajax 的新手,但我很想学习。如果有人能告诉我来自 ajax 的响应与接收浏览器之间的相关性,我只是徘徊。

让它通过 iOS 的 uiwebview 工作将是一个额外的好处。

目前我正在尝试

ajax_handler.js

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();
}

objective-c

JSHandler = [[NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"ajax_handler" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil] retain];


- (void)webViewDidStartLoad:(UIWebView *)webView {
    [webView stringByEvaluatingJavaScriptFromString:JSHandler];
}

我从 here 得到的,我不明白这个 javascript 如何允许我稍后访问从服务器上的 ajax 脚本返回的 ajax 响应

最佳答案

我会尝试为您提供一个简单的演示 - 您可以查看此 ajax 教程 - http://www.w3schools.com/ajax/default.asp

function ajax (url, onsuccess)
{
    var xhr = new XMLHttpRequest();

    xhr.open ("GET", url);
    xhr.send ();

    xhr.onreadystatechange = function (event)
    {
        //alert (xhr.responseText);
        try {
            switch (xhr.readyState) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    //alert (xhr.statusText);
                    //alert (onsuccess);
                    onsuccess (xhr.responseText, xhr.statusText, xhr.status);
                    break;
                default:
                    break;
            }
        }
        catch (e)
        {
        }
    }
}

// call back when ajax is done
function onSuccessCallback (data, statusText, status)
{
    //alert (data);
}

function fetchPage ()
{
    ajax ("http://your domain is here", onSuccessCallback);
}

然后你可以在你的代码上使用它来调用这个函数,这将触发ajax调用,但是ajax是在“异步”模型中工作的(如果你没有指定它同步),所以你只是触发它, 但不会在此方法中得到响应 -

[self.webview stringByEvaluatingJavaScriptFromString:@"fetchPage();"

所以这里有一个 javascript 回调通知你的 webview 的技巧 -

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    // check the request, if it's ajax callback, just return NO; other wise YES;
    NSURLRequest *request = [self.webview request];
    NSURL *url = [request URL];

    NSString *file = [url lastPathComponent];

    if ([file isEqualToString:@"ajaxcallback.htm"] == YES) {
        // that means the ajax call is done, so you can call this 
        NSString *result = [self.webview stringByEvaluatingJavaScriptFromString:@"getResponse ();"];

        // don't forget to stop loading this fake page
        return NO;
    }
}

修改ajax的回调函数——

var response;

function onSuccessCallback (data, statusText, status)
{
    // store the data to global variable
    response = data;

    // trigger webview to load a new page, but actually stop loading in delegate
    window.location = "http://www.domain.com/ajaxcallback.htm";
}

// call this in your objective C code
function getResponse ()
{
    return response;
}

关于javascript - 使用此(或其他 javascript)函数了解 XMLHttpRequest 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8828195/

相关文章:

iphone - 父 View navigationController

javascript - 在 SAPUI5 (Javascript) 项目中集成 Quill 富文本编辑器工具栏

javascript - 如何找到渗透路径并突出显示?

ios - 忽略随机 subview 的 UIAppearance 代理而不是整个类

ios - 在 iOS 设备之间使用空投时如何将文件移出收件箱文件夹

iphone - 在 iPhone 编程中寻找时间

javascript - 如何从nodejs检查Redis缓存连接状态

javascript - D3.js 版本 3 中的 donut 气泡图

ios - 将拖动触摸传播到 UIScrollView super View

iphone - EKEventViewDelegate didCompleteWithAction 没有被调用