javascript - 在html中使用ajax解析xml文件

标签 javascript html ajax

当我使用以下代码时,我得到 xmlhttp.status 0 我搜索了当我将得到 xmlhttp.status 为 0 时我发现如果我使用本地文件,那么我将得到响应为 0 当我运行代码时我是得到的响应为空。

    <script language="javascript">
    var xmlhttp;
    function init() {
       // put more code here in case you are concerned about browsers that do not provide XMLHttpRequest object directly
       xmlhttp = new XMLHttpRequest();
    }
    function getdetails() {
        var txtDate = document.getElementById("txtDate");
        var url = "http://192.168.1.31:8080/CFL/seam/resource/restv1/test?date=" + txtDate.value;
        xmlhttp.open('GET',url,true);
        xmlhttp.send(null);
        xmlhttp.onreadystatechange = function() {
               if (xmlhttp.readyState == 4) {
                  if ( xmlhttp.status == 0) {
                        var det = eval( "(" +  xmlhttp.responseXML + ")");
                var xmlDoc=xmlhttp.responseText;
            alert(xmlDoc);
                 }
                 else
                        alert("Error ->" + xmlhttp.responseText);
              }
        };
    }
  </script>

<body  onload="init()">

    <form name="form1" method="post" action="jquery-datepicker-disable-future-dates.aspx" id="form1">
       <center>Date:&nbsp;&nbsp;&nbsp;&nbsp;<input name="txtDate" type="text" id="txtDate"  /><br /><input type="button" value="Get Details" onclick="getdetails()"/></center>
       <div id="div1"></div>
   </table>
    </form>
</body>

最佳答案

此安全限制可防止 XMLHttpRequest 对象 请求最初提供脚本的域之外的资源。

因此,您必须将上述代码托管在域 192.168.1.31:8080 内的文件中才能使其正常工作,

关于javascript - 在html中使用ajax解析xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9476234/

相关文章:

php - 为什么我的 AJAX GET 请求需要 2-3 秒,而相同的 MYSQL 命令行查询只需要不到 0.1 秒?

javascript - 使用 rowspan 将重复值单元格合并为具有相同空间量的单个单元格

javascript - 使用 Javascript 更改 CSS 可见性属性

javascript - AngularJS 传递范围?

html - 向右浮动将标签文本向上推

c# - 数据网格 asp.net 中的超链接列

php - Ajax请求触发提交....为什么?

javascript - 单击下拉列表中的子 DIV

javascript - Chrome 扩展设置为 `run_at` `document_start` 运行速度太快?

css - 当我将鼠标悬停在我的 div 上时,如何让我的文本消失?