Link to home
Create AccountLog in
Avatar of samrose
samrose

asked on

AJAX xmlhttp Cross Domain issue

Hi experts,

I'm trying to find an answer to the following.

I'm currently using AJAX to call

 xmlhttp.open("POST", "http://192.168.254.110:8080",true);
 xmlhttp.send("<?xml version=\"1.0\"?><methodCall><me.......</methodCall>");

But this is limited because of the cross domain security issue.

I don't have access to PHP on my server (external to 192.168.254.110), so I need another workaround.

Any information will be very valuable to me.
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

One way you can do is to create a "proxy" page in your own web application.
So the AJAX request will go to your proxy page, and the proxy page forward the request accordingly to the remote host.
Btw, just to add on, as far as I know, currently AJAX cannot cross domain yet.
You can use cURL on your server to tunnel AJAX requests through it.
Avatar of samrose
samrose

ASKER

philipjonathan,

Thanks so far, how would I create the proxy in ASP?
Avatar of samrose

ASKER

rohypnol, how can i use cURL?
Avatar of samrose

ASKER

Actually I do have access to PHP on the server if it helps?
The "proxy" is actually just a normal ASP.NET page, that becomes middleman between your AJAX and the remote host.

1. Add a new page to your app

2a. Open the .aspx page in HTML mode.
2b. Delete all lines, except for the first line, <%@ Page ...>

3a. Open your code-behind .aspx.vb (VB) or .aspx.cs (C#)
3b. Add the following code to the Page_Load event handler (in VB, let me know if you use C#). Pls add "imports" namespaces as required.

' Read input stream, from AJAX
Dim inputStream As New StreamReader(Request.InputStream)
Dim requestContent As String = inputStream.ReadToEnd()
 
 
' Open connection to remote host
Dim reqUrl As String = "http://192.168.254.110:8080";
Dim myReq As HttpWebRequest = CType(WebRequest.Create(reqUrl), HttpWebRequest)
myReq.Method = "POST"
Dim sw As New StreamWriter(myReq.GetRequestStream())
sw.Write(requestContent)
sw.Close()
 
 
' Retrieve response
Dim myRes As HttpWebResponse = myReq.GetResponse()
Dim sr As New StreamReader(myRes.GetResponseStream())
Dim responseContent As String = sr.ReadToEnd()
sr.Close()
myRes.Close()
 
 
' Return response to the client AJAX
Response.Write(responseContent)

Open in new window

Avatar of samrose

ASKER

Thanks philipjonathan,

Now I am going to sound silly but how do I call the .aspx page from the AJAX?

I've tried  

xmlhttp.open("GET", "proxy.aspx", true);

And it isn't working
You can still use the same code that you call the remote host, but change the destination URL to your proxy page
 xmlhttp.open("POST", "proxy.aspx",true);
 xmlhttp.send("<?xml version=\"1.0\"?><methodCall><me.......</methodCall>");
Avatar of samrose

ASKER

Thats what I thought, but it unfortunately doesn't work it doesn't call the 192.168.254.110..

I'm thinking I'm going to have to look at another approach, like using a java applet for this and the java equivalent for xmlhttp
Any error message?
Curious, because I've done exactly the same thing and it works. In fact the code above is cut and pasted from my own code.

Here's how you can troubleshoot it:

AJAX --1--> ASPX --2--> Remote host
        <--4--          <--3--

First, troubleshoot 1 and 4.
In my code above, comment out line 6 through 20.
And change line 24 to:
Response.Write(requestContent)

Then make a call from your AJAX. It should receive back the same data that it posted.

Second, troubleshoot 2 and 3.
Use the code as above, and change line 3 to:
Dim requestContent As String = "<?xml version=\"1.0\"?><methodCall><me.......</methodCall>"

Then run Proxy.aspx using your browser, see if you receive the correct response
Avatar of samrose

ASKER

Hi PhilipJonathan again,

Here is the error message that I recieve...


An existing connection was forcibly closed by the remote host
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

Source Error:


Line 24:
Line 25:         ' Retrieve response
Line 26:         Dim myRes As HttpWebResponse = myReq.GetResponse()
Line 27:         Dim sr As New StreamReader(myRes.GetResponseStream())
Line 28:         Dim responseContent As String = sr.ReadToEnd()

Source File: C:\Websites\fimag\proxy.aspx.vb    Line: 26
 
ASKER CERTIFIED SOLUTION
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer