Solved

Calling web service across domain

Posted on 2013-11-22
6
335 Views
Last Modified: 2013-12-27
I want to create javascript which access web service across domain like google ad sense
Please help me with the same
0
Comment
Question by:umeshmodi
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
duncanb7 earned 500 total points
Comment Utility
The Ajax on todays browsers is only   allowed for same domain access,
if you need to cross-domain ,you need a helper page setup in your server in
which it will help to grab the result from  the cross-domin site by for example
like curl() function in php and then it will echo back the result into your javascript code .
The following is just example code and it's not tested. And so  target example
cross-domain, like google, its content html code result will be
collected into your javascript code, and then you can do any javascript operation as you like
Hope I understand your question clearly, otherwise please point out the mistake.

Duncan

Helper page, helper.php
=========================
<?php

exec("curl  ".$_GET['a'].">junk.txt", $a);
echo file_get_contents("junk.txt");


?>

Open in new window

yourjavascript.js, Ajax for get  result by  helper page from your server
=====================================================
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","helper.php?a=http://www.google.com",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

Open in new window

0
 

Author Comment

by:umeshmodi
Comment Utility
My project is in asp.net
0
 
LVL 13

Assisted Solution

by:duncanb7
duncanb7 earned 500 total points
Comment Utility
Dear umeshmodi,

I did not find anything about a word of  "asp.net" in your question and its topic or tag . I got a little bit confusing. So I guess you want the php code is replaced by
asp.net, Right ? Since I don't use asp.net, so the following aspx code  is just example idea or code only and not tested.  I think other EE members will have better help or code in asp.net

Hope I understand your question compeletely, if not, please help to point it out my
mistake

helper.aspx
<% 
    url = Request.QueryString['a']
    set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
    xmlhttp.open "GET", url, false 
    xmlhttp.send "" 
    Response.write xmlhttp.responseText 
    set xmlhttp = nothing 
%>

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now