使用这个(或其他javascript)函数理解XMLHttpRequest响应? [英] understanding XMLHttpRequest responses using this (or other javascript) functions?

查看:157
本文介绍了使用这个(或其他javascript)函数理解XMLHttpRequest响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there anyway of getting iphone/iPad to access XMLHTTPRequests, i.e. ajax responses from a web page within UIWebView. I've tried a couple of solutions out there where the response url is changed so that the app can identify that it is indeed an ajax response.

我是新来的阿贾克斯,但我很想学。如果有人能告诉我ajax和接收浏览器的响应之间的相关性,我只是徘徊。

I'm new to ajax but I'm keen to learn. I just wandered if anyone could tell me the correlation between responses from ajax and the receiving browser.

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

Getting it to work through the iOS's uiwebview would be an added bonus.

到目前为止,我正在尝试

So far I'm trying

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

目标C

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


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

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

which I got from here, I don't understand how this javascript will allow me to later access ajax responses that are returned from the ajax script on the server

推荐答案

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

I will try to give you a simple demo for this - your can check this tutorial for 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工作在异步模型(如果你没有指定它同步),所以你只是触发它,但不会得到这个方法的响应 -

Then you can use this on your code to call this function, this will trigger the ajax call, but the ajax is working in "asynchronous" model (if you don't specify it to sync), so you just trigger it, but will not get the response in this method -

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

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

So here is a trick for doing javascript callback notify your 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的回调函数 -

Modify the callback function of 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)函数理解XMLHttpRequest响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆