Jump to content

Httprequest outside domain


Sean_Tublewicz
 Share

Recommended Posts

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/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<title>TEST PAGE</title>
<head>
<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.
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<title>TEST PAGE</title>
<head>
<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. 
 
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.