XMLHttpRequest onreadystatechange语句后的Javascript对象错误的回调行为 [英] Callback behavior of Javascript Object Error after XMLHttpRequest onreadystatechange statement

查看:130
本文介绍了XMLHttpRequest onreadystatechange语句后的Javascript对象错误的回调行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让h = Af ??????????社交网站上报告链接的价值。我测试这个: http://jsfiddle.net/rtnNd/6/ 检索值,它在我测试的时候有效。

但我不希望用户与之交互。脚本必须自动检索页面并获取它的值。 (我使用Google Chrome Javascript控制台进行测试),当我尝试使用下面的代码进行拼字游戏,并赋值为d并找到h的值,但搜索/替换完全不起作用。为什么getElementsByClassName不起作用?

使用JQuery:

 函数getParameterByName(name,path){
var match = RegExp('[?&]'+ name +'=([^&] *)')。
return match&& decodeURIComponent(match [1] .replace(/ \ + / g,''));
}

var URL ='http://www.facebook.com/zuck';
xmlhttp = new XMLHttpRequest();
xmlhttp.open(GET,URL,true);
xmlhttp.setRequestHeader(Content-type,application / xhtml + xml);
xmlhttp.send();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.status == 404){
result = error404;
}
if(xmlhttp.readyState == 4&&x; xmlhttp.status == 200){
var d = xmlhttp.responseText;
var jayQuery = document.createElement('script');
jayQuery.src =//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js;
document.getElementsByTagName(head)[0] .appendChild(jayQuery); $($。'hidden_​​elem')。d.replace('<! - ','').replace(' - >',''))。find(' .itemAnchor [HREF * = H =] HREF') ')ATTR(';
var fbhvalue = getParameterByName('h',href);
alert(fbhvalue);




$ b $ p
$ b我发现了这个错误:ERROR:Uncaught TypeError:不能调用方法'取代'未定义,即使我添加jQuery。



没有JQuery:

 函数getParameterByName(name,path){
var match = RegExp('[?&]'+ name +'=([^&] *)')。
return match&& decodeURIComponent(match [1] .replace(/ \ + / g,''));
}
var URL ='http://www.facebook.com/zuck';
xmlhttp = new XMLHttpRequest();

xmlhttp.open(GET,URL,true);
xmlhttp.setRequestHeader(Content-type,application / xhtml + xml);
xmlhttp.send();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.status == 404){
// result = error404;
}
if(xmlhttp.readyState == 4&&x; xmlhttp.status == 200){
d = xmlhttp.responseText;
checker(d);



函数检查器(i){
i.value = i;
$ b $ var hiddenElHtml = i.replace('<! - ','').replace(' - >','').find('。itemAnchor [href * = H =] HREF') ')ATTR(';

var divObj = document.createElement('div');
divObj.innerHTML = hiddenElHtml;

var itemAnchor = divObj.getElementsByClassName('itemAnchor')[0];
var href = itemAnchor.getAttribute('href');
var fbId = getParameterByName('h',href);
alert(fbId);






当我运行它的时候,没有找到方法。

为什么getElementsByClassName,getParameterByName,Jquery,getAttribute在xmlhttp.onreadystatechange语句调用后不工作?请你能指出我。有没有办法找到如何获得价值?感谢您阅读我的问题和回答!

更新:
(如果我在个人资料页面上访问,请在Google Chrome的JavaScript控制台中运行以下脚本, h值 - 但我真的不想这样...我想指定用户配置文件的名称,它必须自动检索,并得到它的h值。)

  var jayQuery = document.createElement('script'); 
jayQuery.src =//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js;
document.getElementsByTagName(head)[0] .appendChild(jayQuery);
$ b $ function getParameterByName(name,path){
var match = RegExp('[&]'+ name +'=([^&] *)')。exec路径);
return match&& decodeURIComponent(match [1] .replace(/ \ + / g,''));
}


var href = $($('。hidden_​​elem')[1] .innerHTML.replace('<! - ','').replace (' - >',''))。find('。itemAnchor [href * =h =]')。attr('href');
alert(href);
var fbId = getParameterByName('h',href);
alert(fbId);


解决方案

获得 SOP ,由于安全原因,这是有限的看到这个:

所以,我们假设你得到了的响应字符串 xmlhttp.responseText ,所以你不能使用一些仅适用于DOM对象的JQuery或JavaScript函数,它只是简单的字符串。例如,您使用了不适用于字符串的方法 find() attr()。你可以把它当作一个字符串来玩,没什么其他的了。


I am trying to get h=Af?????????? value from reporting link at social networking site. I test with this : http://jsfiddle.net/rtnNd/6/ to retrieve the value, it works when I tested out.

but I don't want user to interact with it. the script have to auto retrieve the the page and get the value of it. ( I test with Google Chrome Javascript Console) when I try with following coding which does scrabble the page and assign to value d and find the value of h, but search/replace doesn't work at all. Why getElementsByClassName is not working in that?

With JQuery:

function getParameterByName(name, path) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(path);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var URL = 'http://www.facebook.com/zuck';
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", URL, true);
xmlhttp.setRequestHeader("Content-type", "application/xhtml+xml");
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
       if (xmlhttp.status == 404) {
               result = error404;
       }
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
              var d = xmlhttp.responseText;
               var jayQuery = document.createElement('script');
               jayQuery.src = "//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
               document.getElementsByTagName("head")[0].appendChild(jayQuery);
               var href = $($('.hidden_elem').d.replace('<!--', '').replace('-->', '')).find('.itemAnchor[href*="h="]').attr('href');
               var fbhvalue = getParameterByName('h', href);
               alert(fbhvalue);
       }
}

I go this error, "ERROR: Uncaught TypeError: Cannot call method 'replace' of undefined", Even I add the jquery.

Without JQuery:

      function getParameterByName(name, path) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(path);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
var URL = 'http://www.facebook.com/zuck';
xmlhttp = new XMLHttpRequest();

xmlhttp.open("GET", URL, true);
xmlhttp.setRequestHeader("Content-type", "application/xhtml+xml");
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
        if (xmlhttp.status == 404) {
                // result = error404;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                d = xmlhttp.responseText;
                checker(d);
        }
}

function checker(i) {
        i.value = i;

        var hiddenElHtml = i.replace('<!--', '').replace('-->', '').find('.itemAnchor[href*="h="]').attr('href');

        var divObj = document.createElement('div');
        divObj.innerHTML = hiddenElHtml;

        var itemAnchor = divObj.getElementsByClassName('itemAnchor')[0];
        var href = itemAnchor.getAttribute('href');
        var fbId = getParameterByName('h', href);
        alert(fbId);

}

When I run it, I got the error called "object has no method 'find'".

Why getElementsByClassName, getParameterByName, Jquery, getAttribute are not working after xmlhttp.onreadystatechange statement called? please can you point me out. Is there any way to find out how to get value? Thanks for reading my question and answering!

Updated: ( If i visited on profile page, run following script in javascript console of Google Chrome, it gots the h value - but I really don't want that way... I want to specified the user profile name, it has to retrieve automatically and get the h value of it.)

    var jayQuery = document.createElement('script');
    jayQuery.src = "//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
    document.getElementsByTagName("head")[0].appendChild(jayQuery);

    function getParameterByName(name, path) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(path);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    }


    var href = $($('.hidden_elem')[1].innerHTML.replace('<!--', '').replace('-->', '')).find('.itemAnchor[href*="h="]').attr('href');
    alert(href);
    var fbId = getParameterByName('h', href);
    alert(fbId);

解决方案

If you use this script outside the facebook you will definitely get SOP, It is limited due to security reasons see this: Access Control Allow Origin not allowed by

So, Let's imagine you got the response string xmlhttp.responseText, so you cannot use some JQuery or JavaScript functions which only work with DOM objects but it is just plain string. For example, you used find(), attr(), methods which don't work with strings. You can play with it as a string and nothing more.

这篇关于XMLHttpRequest onreadystatechange语句后的Javascript对象错误的回调行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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