简体   繁体   English

Javascript获取XMLHttpRequest的JSON结果

[英]Javascript get JSON result of XMLHttpRequest

I am trying to get the result of some XMLHttpRequest of Jenkins server 我正在尝试获取Jenkins服务器的一些XMLHttpRequest的结果

var oReq = new XMLHttpRequest();
var res = oReq.open("get", "http://" + IP + ":8080/job/Job1/lastBuild/api/json?depth=1", true);
oReq.send();

I am not getting the input to var res however, why is that? 我没有收到var res的输入,为什么呢?

EDIT 编辑

I tried one of the answers that were commented here and I am getting 我尝试了在这里评论的答案之一,我得到了

No 'Access-Control-Allow-Origin' header is present on the requested resource.

What does it means? 这是什么意思?

Thanks 谢谢

1) Use jquery . 1)使用jquery It will help you with a lot of stuff! 它将为您提供很多帮助!

$.getJSON( "http://" + IP + ":8080/job/Job1/lastBuild/api/json?depth=1", function( data ) {
    //data contains full parsed JavaScript object of json 
});

2) Access-Control-Allow-Origin allows the Browser to read informations from a other Origin, bur here is a better answer 2)Access-Control-Allow-Origin允许浏览器从其他起源读取信息,而bur这是一个更好的答案

It's better to use jQuery's $.getJSON, $.ajax, $.get and etc. 最好使用jQuery的$ .getJSON,$。ajax,$。get等。

But if You want native way (don't want to load external libraries or prefer native javascript and etc.) so try this: 但是,如果您想要本机方式(不想加载外部库或更喜欢本机javascript等),请尝试以下操作:

 // returns proper XMLHttpRequest object function getXmlHttp(){ var xmlhttp; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } return xmlhttp; } // creates CORS Request object function createCORSRequest(method, url) { var xhr = getXmlHttp(); if ("withCredentials" in xhr) { xhr.open(method, url, true); } else if (typeof XDomainRequest != "undefined") { xhr = new XDomainRequest(); xhr.open(method, url); } else { xhr = null; } return xhr; } // does request and calls callback function and passes json data to callback function function getJson(url, callback) { var request = createCORSRequest('GET', url); if(!request) { console.log('Cannot create request'); return false; } request.onload = function() { var data = JSON.parse(request.responseText); callback(data); }; request.onerror = function() { console.log('Error happen'); }; request.send(null); } // usage //var url = "http://" + IP + ":8080/job/Job1/lastBuild/api/json?depth=1"; var url = 'http://time.jsontest.com/?alloworigin=true'; getJson(url, function(data) { alert(data.time); console.log(data); }); 


About CORS: http://www.html5rocks.com/en/tutorials/cors/ 关于CORS: http ://www.html5rocks.com/en/tutorials/cors/

Also: there must be defined access rules about CORS in web server with IP that You're requesting. 另外:必须在您请求的IP上定义有关Web服务器中CORS的访问规则。
read about it here: http://enable-cors.org/server_nginx.html , http://enable-cors.org/server_apache.html 在此处阅读有关内容: http : //enable-cors.org/server_nginx.html,http : //enable-cors.org/server_apache.html



also You can add custom headers in serverside application. 您还可以在服务器端应用程序中添加自定义标头。
for example in PHP: 例如在PHP中:

header('Access-Control-Allow-Origin: *');

in RoR: https://gist.github.com/dhoelzgen/cd7126b8652229d32eb4 在RoR中: https//gist.github.com/dhoelzgen/cd7126b8652229d32eb4

open doesn't return anything, check the API reference on MDN . open不返回任何内容,请检查MDN上API参考 Since you're making an async request by sending the third argument as true , it's not going to finish immediately after send() either. 由于您通过将第三个参数发送为true来发出异步请求,因此它也不会在send()之后立即完成。 I'd recommend reading this guide on how to use XMLHttpRequest. 我建议阅读本指南 ,了解如何使用XMLHttpRequest。

You can simply set xhr.responseType = 'json'; 您可以简单地设置xhr.responseType = 'json'; and response will be a JSON. 并且response将是JSON。

 const xhr = new XMLHttpRequest(); xhr.open('GET', 'http://echo.jsontest.com/insert-key-here/insert-value-here/key/value'); xhr.responseType = 'json'; xhr.onload = function(e) { if (this.status == 200) { console.log('response', this.response); // JSON response } }; xhr.send(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM