Solved

Calling web service across domain

Posted on 2013-11-22
6
342 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
ID: 39668740
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
ID: 39671249
My project is in asp.net
0
 
LVL 13

Assisted Solution

by:duncanb7
duncanb7 earned 500 total points
ID: 39671343
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

806 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