?
Solved

Calling web service across domain

Posted on 2013-11-22
6
Medium Priority
?
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
duncanb7 earned 2000 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
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…
Suggested Courses

743 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