0

I'm trying to mash a proof of concept together and have the following rough code snippet in there...

var jenkinsBuilds = [
    {url:"http://jenkins.server:8080/view/project/job/build_type1/", x:100, y:100},
    {url:"http://jenkins.server:8080/view/project/job/build_type2/", x:200, y:200}
];

function createBuild(buildData) {

    var divBuild = document.createElement("div");
    divBuild.className = "buildNode";
    divBuild.title = buildData.url;
    divBuild.style.left = buildData.x + "px";
    divBuild.style.top = buildData.y + "px";
    backdrop.appendChild(divBuild);

    var obj = document.createElement("object");
    obj.onload = loadedObj;
    obj.type = "text/html";
    obj.data = buildData.url + "api/json?tree=builds[number]";
    divBuild.appendChild(obj);
}

function loadedObj(e) {
    alert(e.srcElement);
}

The alert comes up with [object HTMLObjectElement].

I'm wanting to get the json contents of the object into a var json = variable to do a JSON.parse() on later, but I don't know how to get the json contents out of the object.

When I look at the object in the Chrome DOM inspector, it has...

<div>
   L <object>
        L #document
             L <html>
                  L <head></head>
                  L <body>
                       L <pre>
                            "{"builds":[{"number":3431},{"number":3430},{"number":3429},{"number":3428},{"number":3427}]}"
                         </pre>

So I can see the json contents in the innerText of the node in the inspector.

But - the <object> has childElementCount: 0 and the #document has ownerDocument: null and parentNode: null - so how can I get access to the <pre>'s json innerText?

I would prefer to use plain javascript rather than jQuery if possible (for ease of maintainability if I get hit by a truck).

This is not intended for public use, and will be run on the latest browsers (99% Chrome and Firefox).

Thanks for any help.


If I understand correctly, the <object> that you include has a different domain that your main code, and you fall prey to Same-origin policy. You can't access another domain from the browser unless you can edit the other domain and set up CORS or run Chrome with --disable-web-security option.

4

1 回答 1

1

如果我理解正确,您包含的域与您的主代码具有不同的域,并且您会成为Same-origin policy 的<object>牺牲品。除非您可以编辑另一个域并设置CORS或使用选项运行 Chrome,否则您无法从浏览器访问另一个域。--disable-web-security

于 2013-10-03T10:55:56.483 回答