0

I have two servers in my organization. One of which is read-only to me (Server A) and the other hosts our knowledge base (Server B). There is an XML file on Server A which is refreshed at an unknown interval. This file contains information on the status of various items. I want to be able to display those statuses on Server B.

As a beginner, I'm having trouble getting around the same-origin policy since I do not have access to Server A.

Right now I'm trying to use a simple python script xmlpull.py:

import urllib2

response = urllib2.urlopen('http://192.168.255.255/connections')
html = response.read()

The script works great on its own, but the issue is when I try to load it using JQuery (xmlpull.html):

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $.ajax({url: "xmlpull.py", success: function(result){
        $("#2").html(result);
    }});
});
</script>
</head>
<body>
<div id="2">Change Me Please</div>
</body>
</html>

FF just gives me syntax errors for both xmlpull.html and xmlpull.py files at :1:1.

  1. What am I doing wrong?
  2. If this isn't the best way to approach this problem, then feel free to suggest a better way.

Thanks in advance!

A. Kay
  • 23
  • 5
  • Why do you need ajax? Do you need the xml response rendered in browser, specifically html page? Python can do that directly. And do note: xml markup cannot be embedded as is inside html markup to show in browser. You need to transform xml to html. – Parfait Sep 22 '16 at 02:13
  • @Parfait I guess I don't need AJAX? I was following the instructions [here](http://stackoverflow.com/questions/13175510/call-python-function-from-javascript-code). How else would I do it? – A. Kay Sep 22 '16 at 02:21
  • What is your desired result? I can't see the XML. Do you want HTML table of results? – Parfait Sep 22 '16 at 02:27
  • do you have apache mod wsgi or mod python to be able to access the python file from the ajax request ? – Amr Magdy Sep 22 '16 at 02:52
  • @Parfait Desired result is to display status (True/False) of various objects with the information from the XML. – A. Kay Sep 22 '16 at 03:07
  • @AmrMagdy I do not and that's why I need your help. Can you point me to resources with info on how to implement these? – A. Kay Sep 22 '16 at 03:09

1 Answers1

0

You have 3 Options options:

  • First: is to allow Server B to access Server A. If you are using apache server you can do this by add this code the apache configuration file and restart apache

    SetEnvIf Origin "http(s)?://(www.)?(WRITE_IP_OF_SERVER_B_HERE)$" AccessControlAllowOrigin=$0 Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin Header set Access-Control-Allow-Headers "Content-Type, Accept, Authorization, X-Requested-With"

This way you can call Server A from Server B direct from the js and no need to the to create python file.

  • Second using the python file:
    to call python file from ajax, the python file should be accessable using apache or other server and to do this you have two options. mode wsgi or mod python and both will require apache configuration and code change more than the code size
  • Third way if to use PHP for this task by creating a php file and add it to apache public folder ( www ) you can call it direct without apache configuration.
    file content will be:

<?= file_get_contents("http://192.168.255.255/connections"); ?>

Amr Magdy
  • 1,710
  • 11
  • 13