Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calling web service across domain

Posted on 2013-11-22
6
Medium Priority
?
353 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
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)

604 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