Solved

Calling web service across domain

Posted on 2013-11-22
6
337 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)

911 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

21 Experts available now in Live!

Get 1:1 Help Now