开发者

How do I convert my server-side ASP XmlHttpRequest code to client-side JavaScript?

开发者 https://www.devze.com 2022-12-20 16:30 出处:网络
i using below asp code for xml request <% pXML=Server.URLencode(SearchRequest) set xmlhttp = server.CreateObject(\"MSXML2.ServerXMLHTTP\")

i using below asp code for xml request

<%
pXML=Server.URLencode(SearchRequest)
set xmlhttp = server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhtt.open "post", http://OutSideDomain/xml_requests , false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send  "xml_request=" &pXML
htresult = xmlhttp.responsexml.xml    
%>

I try to write the above code in javascript(Ajax)

<script type="text/javascript">
function loadXMLDoc()
{
var xmlHttp;

try
  {  
  xmlHttp=new XMLHttpRequest();  }
catch (e)
  { 
   try
    {    
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
     }
  catch (e)
    {   
     try
      {     
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");   
          }
    catch (e)
      {      
      alert("Your browser does not support AJAX!");      
      return false; 
           }    
           } 
            }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      alert(xmlHttp.responseText);
      }
    }   

    var params ='xml_request=' +'<%=pXML>';
    xmlHttp.open("POST","http://OutSideDomain/xml_requests",true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);

   }
</script>

Th开发者_JAVA技巧e above asp code is working fine and get xml response,

But in the javascript i getting error is "Access Denied" in the like xmlHttp.send(params);

what is the problem for "Access Denied"?,

I here that from one HTTP to another HTTP is not possible ... But that thing is working in my asp page..

How i solve this ?

hoping your response


The problem is the difference in context. Your JavaScript code is running client-side and so it's subject to the Same Origin Policy. Your ASP code is running server-side and so it isn't.

You basically can't do this from the client right now, although there are initiatives going on that will make some limited amount of cross-site communication possible. For the moment, you're better off continuing to do this on the server-side (despite the bandwidth cost that will cause).

Alternately, if the source you're trying to reach provides a JSONP interface, you can use that.

0

精彩评论

暂无评论...
验证码 换一张
取 消