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\"?><methodCa ll><me.... ...</metho dCall>");
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.
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\"?><methodCa
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.
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.
ASKER
philipjonathan,
Thanks so far, how would I create the proxy in ASP?
Thanks so far, how would I create the proxy in ASP?
ASKER
rohypnol, how can i use cURL?
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.
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)
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
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\"?><methodCa ll><me.... ...</metho dCall>");
xmlhttp.open("POST", "proxy.aspx",true);
xmlhttp.send("<?xml version=\"1.0\"?><methodCa
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
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(requestCont ent)
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\"?><methodCa ll><me.... ...</metho dCall>"
Then run Proxy.aspx using your browser, see if you receive the correct response
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(requestCont
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\"?><methodCa
Then run Proxy.aspx using your browser, see if you receive the correct response
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.SocketE xception: 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.GetResp onseStream ())
Line 28: Dim responseContent As String = sr.ReadToEnd()
Source File: C:\Websites\fimag\proxy.as px.vb Line: 26
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.SocketE
Source Error:
Line 24:
Line 25: ' Retrieve response
Line 26: Dim myRes As HttpWebResponse = myReq.GetResponse()
Line 27: Dim sr As New StreamReader(myRes.GetResp
Line 28: Dim responseContent As String = sr.ReadToEnd()
Source File: C:\Websites\fimag\proxy.as
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
So the AJAX request will go to your proxy page, and the proxy page forward the request accordingly to the remote host.