Jump to content


Photo

Httprequest outside domain


  • Please log in to reply
7 replies to this topic

#1 Sean_Tublewicz

Sean_Tublewicz

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 09 August 2014 - 03:48 PM

I'm sorry if this is in the wrong section. I'm working on coding a page that will pull data from a servlet that's already been coded. Right now I'm just trying to get it to pull and display any information. The developer who coded the servlet says it works on his server and I'd rather not badger him with questions. Any help you can provide would be greatly appreciated. So I am running Wampserver 2.5 a basic installation as I have not changed any settings and my HTML page is as follows: 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html>
<title>TEST PAGE</title>
<head>
<script type="text/javascript" src="https://ajax.googlea...n.js"></script>
<script type="text/javascript">
 
getJSON = function() {
    var resultDiv = $("#resultDivContainer");
    resultDiv.html('Loading...');
    
    var request = {
      version: "0.4", 
game: "XXXXXXXXX", 
platform: "XXXXXXXXX", 
userId: "XXXXXXXXX", 
password: "XXXXXXXXX",
data: {}
  };
 
  $('#sendInput').val(JSON.stringify(request));
 
  resultDiv.html(console.log('Sending: ' + $('#sendForm').serialize()));
 
    $.ajax({
        url: "XXXXXXXXX",
        type: "POST",
        data: $('#sendForm').serialize(),
        dataType: "json",
        success: function (result) {
        var resultString = JSON.stringify(result); 
        console.log('Result: ' + resultString);
        resultDiv.html(resultString);
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
        }
    });
};
 
</script>
</head>
<body>
 
<h1>Test Page</h1>
 
<form id="sendForm" action="post"><input id="sendInput" type="hidden" name="json" value="empty" /></form>
 
<div id="resultDivContainer"></div>
 
<button type="button" onclick="getJSON()">JSON</button>
 
</body>
</html>
 
This is supposed to display some JSON results from a tournament that is ongoing. When I click the JSON button it'll change the resultDivContainer to say "Loading..." and then it gives me a popup that says: "The page at localhost says: 0". Is it because I'm not on the same domain as the servlet? Thoughts? Please and thank you.


#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,036 posts
  • LocationWA

Posted 10 August 2014 - 01:30 AM

Probably. The browser's console/error log might give you more information.

If it is the cross-domain issue you can "work around" it by doing the request in PHP code and having your AJAX pull from that instead.

#3 Sean_Tublewicz

Sean_Tublewicz

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 10 August 2014 - 07:07 AM

Hello and thank you for the response requinix. I checked the javascript log and was presented with 

 

XMLHttpRequest cannot load XXXXXXXXXXXXx. The 'Access-Control-Allow-Origin' header has a value 'http://localhost:8888' that is not equal to the supplied origin. Origin 'http://localhost' is therefore not allowed access.

 

My question is... Is this on my end or the servlet I'm trying to access?



#4 Sean_Tublewicz

Sean_Tublewicz

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 10 August 2014 - 02:01 PM

Would it be easier if I can just use PHP for the whole deal? That is fine by me. Unfortunately I can't find many examples that apply to my situation. Does anyone know of any or can you write up a basic one? I don't believe the server is setup to use cURL either. Thanks guys.



#5 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,036 posts
  • LocationWA

Posted 10 August 2014 - 05:45 PM

You're sending requests to yourself? It sounded like you were going to some completely different web site/service.

If you can do anything to the server running on localhost:8888 then that's easiest: you'd add a Access-Control-Allow-Origin header like the error message suggests and everything will work again. Once you deploy it to the internet you'll need to tweak it again (for the new domain name) but it would still work.

#6 Sean_Tublewicz

Sean_Tublewicz

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 10 August 2014 - 06:42 PM

Does the browser log make you think I'm sending requests to myself? I am indeed trying to access a servlet sitting halfway around the world. I tried accessing the local HTML file using http://localhost:8888/test.html to see if it was on my end though it still did not work. I was wondering if just accessing the page using PHP would be easier.



#7 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,036 posts
  • LocationWA

Posted 10 August 2014 - 08:07 PM

Alright...

Unless the other server explicitly allows it, you can't use AJAX to access anything on a different domain name or port. So a page on localhost can't access localhost:8888.
Which means the PHP thing I mentioned. Put a page on localhost that proxies localhost:8888 - using cURL, most likely. Which may mean you can move a lot of the Javascript data (username, password, etc) into the PHP page instead, making things a little safer.

#8 Sean_Tublewicz

Sean_Tublewicz

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 11 August 2014 - 08:28 PM

Hello Requinix. I think I'm getting closer. Now instead of the alert box I just get NULL to show up in the divContainer. 

 

here is proxy.php:

 

<?php
if (!isset($_GET['url']))
{
die();
}
$url=urldecode($_GET['url']);
$url='http://' . str_replace('http://','',$url);
echo file_get_contents($url);
?>
 
and here is my HTML file:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html>
<title>TEST PAGE</title>
<head>
<script type="text/javascript" src="https://ajax.googlea...n.js"></script>
<script type="text/javascript">
 
getJSON = function() {
    var resultDiv = $("#resultDivContainer");
    resultDiv.html('Loading...');
    
    var request = {
      version: "0.4", 
game: "XXXXXXXXX", 
platform: "XXXXXXXXX", 
userId: "XXXXXXXXX", 
password: "XXXXXXXXX",
data: {}
  };
 
  $('#sendInput').val(JSON.stringify(request));
 
  resultDiv.html(console.log('Sending: ' + $('#sendForm').serialize()));
 
    $.ajax({
        url: "proxy.php?url=XXXXXXXXX",
        type: "POST",
        data: $('#sendForm').serialize(),
        dataType: "json",
        success: function (result) {
        var resultString = JSON.stringify(result); 
        console.log('Result: ' + resultString);
        resultDiv.html(resultString);
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
        }
    });
};
 
</script>
</head>
<body>
 
<h1>Test Page</h1>
 
<form id="sendForm" action="post"><input id="sendInput" type="hidden" name="json" value="empty" /></form>
 
<div id="resultDivContainer"></div>
 
<button type="button" onclick="getJSON()">JSON</button>
 
</body>
</html>
 
As I said it appears to communicate with the servlet... and just enters null into the resultDivContainer and the browser javascript log says sending blah blah blah and result: null. Thanks for the help so far and hopefully with your help I can get this going. 
 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com