简体   繁体   English

如何将服务器名称设置为xml文件中javascript中的全局变量?

[英]How to set the server name as a global variable in javascript from an xml file?

I have a web application which makes some Ajax calls and I want to set the server domain name to be used for the ajax calls in an XML configurations file. 我有一个Web应用程序,该应用程序进行了一些Ajax调用,并且我想在XML配置文件中设置要用于ajax调用的服务器域名。 I have seen this tutorial which shows how to parse xml file in javascript. 我看过本教程该教程显示了如何在javascript中解析xml文件。 But the problem is that I need to make an Ajax call to parse the xml file and for that I need to know the server domain name (which I am already trying to fetch from the xml). 但是问题是我需要进行Ajax调用来解析xml文件,为此,我需要知道服务器域名(我已经在尝试从xml中获取该域名)。

So how can I set the server name in an XML file and use to make ajax calls? 那么,如何在XML文件中设置服务器名称并用于进行Ajax调用呢? Is there any other way I can configure the server name outside the javascript file itself? 还有其他方法可以在javascript文件本身之外配置服务器名称吗?


EDIT: 编辑:

Can I do something like this (in which I would not need to set the server name at all and wont need config file) : 我可以做这样的事情吗(其中我根本不需要设置服务器名称,也不需要配置文件):

 function getMetricNotifications()
{    
 var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","/rest/general/notifications/metrics",true);
    xmlhttp.send();
    var response = JSON.parse(xmlhttp.responseText);
    return response;
}

Without mentioning the domain name localhost:8080 (locally) or http://mysite.com:8080 (on server) at the beginning of the URL in xmlhttp.open("GET","/rest/general/notifications/metrics",true); xmlhttp.open("GET","/rest/general/notifications/metrics",true); URL的开头未提及域名localhost:8080 (本地)或http://mysite.com:8080 (在服务器上) xmlhttp.open("GET","/rest/general/notifications/metrics",true);

I tried it and I got : uncaught syntaxerror unexpected end of input before the last line. 我尝试了一下,但得到了: uncaught syntaxerror unexpected end of input最后一行之前的uncaught syntaxerror unexpected end of input Any thoughts? 有什么想法吗?


2nd EDIT: 第二次编辑:

function getMetricNotifications(){   
 var xmlhttp;
 var response = '';
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
             response = JSON.parse(xmlhttp.responseText);
             return response;
          }
      };
    xmlhttp.open("GET","/rest/general/notifications/metrics",true);
    xmlhttp.send(); 
}

Now I'm calling this function and printing the result and it prints "undefined", although accessing the rest resource directly in the browser gives the correct JSON response. 现在,我调用此函数并打印结果,尽管未在浏览器中直接访问其余资源会给出正确的JSON响应,但它会打印“ undefined”。 Why it is not getting the response? 为什么没有得到回应?

It depends on which backend technology do you run on. 这取决于您运行哪种后端技术。 You need to parse the XML file on the server side and insert the server name in its output. 您需要在服务器端解析XML文件,并将服务器名称插入其输出。

In PHP, you could do something like 在PHP中,您可以执行以下操作

<html>
<head><!-- rest of head -->
<script>
<?php $xml = simplexml_load_file('cfg.xml'); ?>
var server_name = <?= json_encode($xml->servername) ?>
</script>

Then you could user server_name in your JavaScript code. 然后,您可以在JavaScript代码中使用server_name

Ad the EDIT: 广告编辑:

Yes, this is the preferred URL format in most cases. 是的,在大多数情况下,这是首选的URL格式。

The error you are seeing is probably caused by a malformed/incomplete JSON input. 您看到的错误可能是由JSON输入格式错误/不完整引起的。

The request you are performing is asynchronous so you need to wait for the data to be available/downloaded. 您正在执行的请求是异步的,因此您需要等待数据可用/下载。 The xhr.responseText is empty right after the xhr.send() call. xhr.send()调用之后, xhr.responseText为空。 Wait for the request to get to the ready state of 4 (complete). 等待请求进入就绪状态4(完成)。

// ...
xhr.onreadystatechange = function () {
  if (xhr.readyState === 4) {
    var data = JSON.parse(xhr.responseText);
    // ... do something with data ...
  }
};
xhr.send(null);

Ad the second EDIT: 广告第二个EDIT:

I suppose you use the function in a synchronous manner such as 我想您以同步方式使用该功能,例如

var data = getMetricNotifications();

You cannot do that if you're getting the data asynchronously. 如果要异步获取数据,则不能这样做。 You need to get familiar with the concept of callback functions and do this instead: 您需要熟悉回调函数的概念,并改为执行以下操作:

getMetricNotifications(function (data) {
  // work with the received data
});

Instead of returning the data, you pass it to the callback function like this: 无需返回数据,而是将其传递给回调函数,如下所示:

var getMetricNotifications = function (callback) {
  // ... do the call ...
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && ...) {
      var data = JSON.parse(xhr.responseText);
      callback(data);
    }
  };
  // ... 
};

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

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