Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 请求Web服务以获取JSON_Javascript_Ajax_Json_Web Services - Fatal编程技术网

Javascript 请求Web服务以获取JSON

Javascript 请求Web服务以获取JSON,javascript,ajax,json,web-services,Javascript,Ajax,Json,Web Services,我需要点击提供JSON对象的web服务URL并使用基本身份验证。如何在JavaScript中执行相同的操作。我正在使用下面的代码,但在浏览器上什么也没发生 <html> <head> <title>Hello World</title> <script> function send_with_ajax() var httpRequest = new XMLHttpRequest(); httpRequest.open

我需要点击提供JSON对象的web服务URL并使用基本身份验证。如何在JavaScript中执行相同的操作。我正在使用下面的代码,但在浏览器上什么也没发生

<html>
<head>
<title>Hello World</title>
<script>

function send_with_ajax()

    var httpRequest = new XMLHttpRequest();
    httpRequest.open("GET", 'url of service', true);
    httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    httpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    httpRequest.send();

    httpRequest.onload= function() { 
        if (httpRequest.readyState == 4)
        {
            var _tempRecommendations = httpRequest.responseXML;
            window.alert(httpRequest.response);
            window.alert(httpRequest.responseText);
            window.alert(_tempRecommendations);
        }
    };  
};

</script>
</head>
<body onload="send_with_ajax()"></body>
</html>

你好,世界
函数send_with_ajax()
var httpRequest=new XMLHttpRequest();
open(“GET”,“服务的url”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
setRequestHeader(“X-request-With”,“XMLHttpRequest”);
httpRequest.send();
httpRequest.onload=函数(){
if(httpRequest.readyState==4)
{
var_tempRecommensions=httpRequest.responseXML;
window.alert(httpRequest.response);
window.alert(httpRequest.responseText);
窗口警报(临时建议);
}
};  
};

检查jQuery以了解此操作。这会导致类似的结果

    $.get("http://yourRestprovider.com/yourResource",function(data){
    //handle your data 
    },"json");
请指定基本身份验证的含义,因为我在代码片段中没有看到针对服务器进行身份验证的方法

(这不需要JQuery。)

首先确保内部函数正在运行,因此尝试将函数设置为
onreadystatechange
而不是
onload


至于身份验证:如果您的域(替换“服务url”的内容)与脚本所在的页面具有相同的来源,并且您已经在进行某种身份验证,其中存储了登录会话cookie,则无需执行任何其他操作。

您好,我正在使用以下代码:之后,它只显示浏览器上的加载

<html><head>
<title>Hello World</title>
<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.js">
</script>
<script type="text/javascript">

function send_with_ajax()
{

        $.ajax  
        ({  
          type: "GET",  
          url: "https://url with params",  
          dataType: 'json',  
          async: false,  
          username: 'abc',  
          password: 'def',  
          data: '{ "comment" }',  
          success: function (){  
            alert('Success!');   
          }  
        });  
}

</script>
</head>
<body onload="send_with_ajax()"></body>
</html>

你好,世界
函数send_with_ajax()
{
$.ajax
({  
键入:“获取”,
url:“https://url 使用参数“,
数据类型:“json”,
async:false,
用户名:“abc”,
密码:“def”,
数据:“{”注释“}”,
成功:函数(){
警惕(“成功!”);
}  
});  
}

设置用户名和密码。您好,如果我使用onreadystatechange,我会收到警报:response and responseText(警报中没有任何内容)和_tempRecommensions为空。我如何解决此问题。抱歉,我不清楚,观察到的行为是什么_我知道,这是空的。你看到任何警报了吗?空警报?但更重要的是,域名匹配吗?您是否在本地计算机上测试html文件?代码已更新,但仍然无法获取json。