如何使用javascript从网址获取“h”值? (ajax,jquery等)
网址:http://www.youtube-mp3.org/a/itemInfo/?video_id=p8-pP4VboBk
网址内容:
info = { "title" : "Laura Branigan - Self Control", "image" : "http://i.ytimg.com/vi/p8-pP4VboBk/default.jpg", "length" : "5", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "http://ping.aclst.com/ping.php/2452159/p8-pP4VboBk?h=761944", "h" : "83135b0b3cf927b5e6caf1cf991b66b3" };
目的是从外部网址;
获取并打印此值(h)83135b0b3cf927b5e6caf1cf991b66b3
答案 0 :(得分:1)
你可以试试这个。我认为这更简单
info.h
答案 1 :(得分:0)
上面的字符串是一个完整的JavaScript对象,您可以将其视为哈希表或关联键数组,其中"key":"value"
如果要索引到此数组或对象,只需使用它键即可Object['key']
其中key可以是int或字符串或任何对象
答案 2 :(得分:0)
Json对象完美地集成在javascript中。
这意味着对于定义:
var myObject = { "name": "Doe", "parents": {"father": "Louis", "mother": "Ophelia"}};
您只需使用以下语句访问数据:
var myName = myObject.name;
var myFather = myObject.parents.father;
var myMother = myObject.parents.mother;
真的很简单。
作为对您问题的准确答案,确实是info.h
。
更新:
如果你的意思是来自info.pf
网址的h,那就是
var h = info.pf.substr(info.pf.indexOf("?h=")+3, 999);
答案 3 :(得分:0)
哦!现在我理解你的问题。 您不知道如何将URL中的响应转换为javascript变量。 你需要一个小的ajax脚本:
var youTubeUrl = "http://www.youtube-mp3.org/a/itemInfo/?video_id=p8-pP4VboBk";
var request = makeHttpObject();
request.open("GET", youTubeUrl, true);
request.send(null);
request.onreadystatechange = function() {
if (request.readyState == 4){
eval(request.responseText);//this should create the info variable.
alert(info.h); //<<<---this should be it!
//TODO: add your code to handle the info.h here.
}
};
function makeHttpObject() {
try {return new XMLHttpRequest();}
catch (error) {}
try {return new ActiveXObject("Msxml2.XMLHTTP");}
catch (error) {}
try {return new ActiveXObject("Microsoft.XMLHTTP");}
catch (error) {}
throw new Error("Could not create HTTP request object.");
}
代码主要来自:https://stackoverflow.com/a/6375580/1311434
请注意,我没有测试过这段代码,但我希望它能让你朝着正确的方向前进。 确保您可以信任从原始URL检索的代码,因为它对您从其他站点检索的eval()代码有点危险。