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

时间:2021-10-17 12:33:20

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. I have seen this tutorial which shows how to parse xml file in javascript. 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).

我有一个Web应用程序,它进行一些Ajax调用,我想设置服务器域名用于XML配置文件中的ajax调用。我已经看过这个教程,它展示了如何在javascript中解析xml文件。但问题是我需要进行Ajax调用来解析xml文件,为此我需要知道服务器域名(我已经尝试从xml中获取)。

So how can I set the server name in an XML file and use to make ajax calls? Is there any other way I can configure the server name outside the javascript file itself?

那么如何在XML文件中设置服务器名称并用于进行ajax调用?有没有其他方法可以在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”中的URL开头提及域名localhost:8080(本地)或http://mysite.com:8080(在服务器上) ,真正);

I tried it and I got : uncaught syntaxerror unexpected end of input before the last line. Any thoughts?

我试了一下,得到了:在最后一行之前未被捕获的syntaxerror意外结束输入。有什么想法吗?


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. Why it is not getting the response?

现在我正在调用此函数并打印结果并打印出“未定义”,尽管直接在浏览器中访问其余资源会给出正确的JSON响应。为什么没有得到答复?

1 个解决方案

#1


1  

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. Wait for the request to get to the ready state of 4 (complete).

您正在执行的请求是异步的,因此您需要等待数据可用/下载。在xhr.send()调用之后,xhr.responseText立即为空。等待请求进入准备状态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:

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);
    }
  };
  // ... 
};

#1


1  

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. Wait for the request to get to the ready state of 4 (complete).

您正在执行的请求是异步的,因此您需要等待数据可用/下载。在xhr.send()调用之后,xhr.responseText立即为空。等待请求进入准备状态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:

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);
    }
  };
  // ... 
};