473,734 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JSON - Cross domain request

I am looking to make a small web app that will return the status of a
website from the client browser. I tried this with AJAX and it worked
great locally, but did not work for remote users accessing the page, I
ran into the security problem with making a cross domain request.

I have been reading that with JSON a cross domain request can be
accomplished, but have not been able to find any examples on how to use
it or how to get the return code from retrieving a web page.

I want to make these requests from the client browser, and it will not
work as a proxy request from my web server. Any ideas on how to make
this work? Please provide a working example if available...
Thanks

Apr 13 '06 #1
13 12409


tr****@gmail.co m wrote:
I want to make these requests from the client browser, and it will not
work as a proxy request from my web server. Any ideas on how to make
this work?


An image object or an img element object can be used with an onload and
onerror handler e.g.
var img = new Image();
img.onerror = function (evt) {
// handle error case
};
img.onload = function (evt) {
// handle load
};
// now try to load a test image on that server you want to check e.g.
img.src = 'http://example.com/test.gif?time=' + new Date().getTime( );
That requires that you can put an image on the server you want to check.
And you don't get any status code, you have to live with either onerror
or onload being fired.

A different possible approach is to put or generate a script on that
server e.g.
<script type="text/javascript"
src="http://example.com/status.js"></script>
that at least allows you to load a resource from a server being a
different origin than the server the HTML document comes from, e.g. you
could do
var serverStatus = false;
with inline script and then status.js on the server sets
serverStatus = true;

I don't see how JSON would help to work around the same origin policy,
it is just a data exchange format that does not help you to make
requests to servers you can't access due to security restrictions.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 13 '06 #2
Your examples make sense, but not exactly what I am looking for.
Basically I want a test page that will give the status of a webpage
based on the client experience. So I would want to status sites like
http://www.yahoo.com or http://www.google.com for the return code. If
you visit the page you would get a 200 return code, but if I visit the
page I might get a different return code because of proxy issues, or
some other local network issue. I want to be able to compare the user
experience from different locations.

I found this on the Yahoo site, but I could not find any example on how
to implement:

Use JSON and dynamic <script> tags instead of XML and XMLHttpRequest.
You can get around the browser security problem altogether by making
your web services request directly inside a <script> tag. If the Yahoo!
Web Service you're using can output JSON (using the output=json and
callback=functi on parameters), the data you get back from the web
service is evaluated as a JavaScript object when the page is loaded.

How could I use the above to accomplish what I am trying to do?? Thanks!

Apr 13 '06 #3


tr****@gmail.co m wrote:

Use JSON and dynamic <script> tags instead of XML and XMLHttpRequest.
You can get around the browser security problem altogether by making
your web services request directly inside a <script> tag. If the Yahoo!
Web Service you're using can output JSON (using the output=json and
callback=functi on parameters), the data you get back from the web
service is evaluated as a JavaScript object when the page is loaded.

How could I use the above to accomplish what I am trying to do??


What they suggest there is the use of a script element to load a script
from that server, what I had already suggested, only that they seem to
intend to dynamically create the script element (e.g.
document.create Element('script ')
) and then have the loaded script call a defined callback function in
your page. My example suggestion was a bit simpler, just a global
variable was used for the data exchange, they want you to use a callback
function the script can call and pass in data in the JSON format. That
is of course possible, only it does not help you to get any status from
that server if there is no special script available on the server that
you can load. So what they do is offer web services you or anyone else
can call using a script element while you want to get a status of any
server where there is no script available.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 13 '06 #4
tr****@gmail.co m writes:
I have been reading that with JSON a cross domain request can be
accomplished,


That sounds like someone misunderstandin g the concept.
JSON is a format for representing data. It does not make any
assumptions about how those data are transferred between computers
(if at all).

What someone might be thinking of is that a <script src="..."> element
can load Javascript from a different source than the containing
document. JSON won't help you there, since loading a JSON expression
as a script element has no measureable effect (unless you can read the
text of the script element after it is loaded, but not all browsers
support that). You can send standard Javascript with side effects,
but then it's not JSON any more.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 14 '06 #5
Lasse Reichstein Nielsen said the following on 4/13/2006 11:04 PM:
tr****@gmail.co m writes:
I have been reading that with JSON a cross domain request can be
accomplished,


That sounds like someone misunderstandin g the concept.


<URL: http://borkweb.com/story/look-ma-cross-domain-scripting >

I think a lot of that misunderstandin g comes, in part, from that
website. It implies that JSON is the secret to it when it isn't. The
secret to it is dynamic script tags.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 14 '06 #6
Randy Webb <Hi************ @aol.com> writes:
<URL: http://borkweb.com/story/look-ma-cross-domain-scripting >

I think a lot of that misunderstandin g comes, in part, from that
website. It implies that JSON is the secret to it when it isn't. The
secret to it is dynamic script tags.


True. It's the Yahoo web-service format that uses JSON, but what it
sends is not pure JSON, but a function call with an argument that is a
JSON expression. It's good use of JSON, but it's not just that. It could
send any Javascript if it wanted, and it would still work as well.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 14 '06 #7

tr****@gmail.co m napisal(a):
I want to make these requests from the client browser, and it will not
work as a proxy request from my web server. Any ideas on how to make
this work? Please provide a working example if available...


First i agree with my followers... another way to get status code is to
write java applet and put it as object with width and height equal to 0
px and visibility to hidden (css). Inside java aplet you can write
method to make call to any server you want, and call that method from
JavaScript using LiveConnect. Example:

ScriptableClock .java
-----------------------------
public void setTimeZone(Str ing zone) {
stop();
timeZone = (zone.startsWit h("GMT")) ? true : false;
start();
}
public void setFont(String newFont, String newStyle, String newSize) {
stop();
if (newFont != null && newFont != "")
fontName = newFont;
if (newStyle != null && newStyle != "")
setFontStyle(ne wStyle);
if (newSize != null && newSize != "")
setFontSize(new Size);
displayFont = new Font(fontName, fontStyle, fontSize);
start();
}
public void setColor(String newbgColor, String newfgColor) {
stop();
bgColor = parseColor(newb gColor);
fgColor = parseColor(newf gColor);
start();
}
public String getInfo() {
String result = "Info about ScriptableClock .class\r\n";
result += "Version/Date: 1.0d1/2 May 1996\r\n";
result += "Author: Danny Goodman (da****@dannyg. com)\r\n";
result += "Public Variables:\r\n" ;
result += " (None)\r\n\r\n" ;
result += "Public Methods:\r\n";
result += " setTimeZone(\"G MT\" | \"Locale\")\r\n ";
result += " setFont(\"fontN ame\",\"Plain\" |\"Bold\" |
\"Italic\",
\"fontSize\")\r \n";
result += " setColor(\"bgCo lorName\",
\"fgColorName\" )\r\n";
result += " colors: Black, White, Red, Green, Blue, Yellow\r\n";
return result;
}

ScriptableClock .html
------------------------------
<html>
<head>
<title>Clock with Lots o' Widgets</title>
<script type="text/javascript">

function setTimeZone(pop up) {
var choice = popup.options[popup.selectedI ndex].value;
document.clock2 .setTimeZone(ch oice);
}

function setColor(form) {
var bg = form.background Color.options[
form.background Color.selectedI ndex].value;
var fg = form.foreground Color.options[
form.foreground Color.selectedI ndex].value;
document.clock2 .setColor(bg, fg);
}

function setFont(form) {
var fontName = form.theFont.op tions[form.theFont.se lectedIndex].value;
var fontStyle = form.theStyle.o ptions[
form.theStyle.s electedIndex].value;
var fontSize = form.theSize.op tions[form.theSize.se lectedIndex].value;
document.clock2 .setFont(fontNa me, fontStyle, fontSize);
}

function getAppletInfo(f orm) {
form.details.va lue = document.clock2 .getInfo();
}

function showSource() {
var newWindow = window.open("Sc riptableClock.j ava","",
"width=450,heig ht=300,resizabl e,scrollbars");
}

</script>
</head>
<body>
<applet code="Scriptabl eClock.class" name="clock2"
width="500"
height="45">
<param name="bgColor" value="Black" />
<param name="fgColor" value="Red" />
</applet>
<form name="widgets2" >
Select Time Zone: <select name="zone"
onchange="setTi meZone(this)">
<option selected="selec ted" value="Locale"> Local Time</option>
<option value="GMT">Gre enwich Mean Time</option>
</select>
<p>Select Background Color: <select name="backgroun dColor"
onchange="setCo lor(this.form)" >
<option value="White">W hite</option>
<option selected="selec ted" value="Black">B lack</option>
<option value="Red">Red </option>
<option value="Green">G reen</option>
<option value="Blue">Bl ue</option>
<option value="Yellow"> Yellow</option>
</select> Select Color Text Color: <select name="foregroun dColor"
onchange="setCo lor(this.form)" >
<option value="White">W hite</option>
<option value="Black">B lack</option>
<option selected="selec ted" value="Red">Red </option>
<option value="Green">G reen</option>
<option value="Blue">Bl ue</option>
<option value="Yellow"> Yellow</option>
</select></p>
<p>Select Font: <select name="theFont"
onchange="setFo nt(this.form)">
<option selected="selec ted" value="TimesRom an">Times
Roman</option>
<option value="Helvetic a">Helvetica </option>
<option value="Courier" >Courier</option>
<option value="Arial">A rial</option>
</select><br />
Select Font Style: <select name="theStyle"
onchange="setFo nt(this.form)">
<option selected="selec ted" value="Plain">P lain</option>
<option value="Bold">Bo ld</option>
<option value="Italic"> Italic</option>
</select><br />
Select Font Size: <select name="theSize"
onchange="setFo nt(this.form)">
<option value="12">12</option>
<option value="18">18</option>
<option selected="selec ted" value="24">24</option>
<option value="30">30</option>
</select></p>
<hr />
<input type="button" name="getInfo" value="Applet Info"
onclick="getApp letInfo(this.fo rm)" />
<p><textarea name="details" rows="11" cols="70">
</textarea></p>
</form>
<hr />
</body>
</html>

Here above it is only example, the classes you really need to use is
Socket Java class and emulate HTTP GET or POST or use simple Jakarta
HttpClient (see google).

Hope it helps.

BR
Luke M.

Apr 14 '06 #8
Thanks, the above example makes sense, the only question I have though
is about writting methods within the applet to get the site return
code. If I use 'simple Jakarta HttpClient' within the applet, then when
a call is made to a website for the status, would this call be from the
server, or client browser? I apoligize ahead of time as I am quite a
bit rusty with applets and have had no experience with Jakarta
HttpClient. All I want to accomplish is to gather the return codes from
several sites from the client browser experience. Thanks for your help!

Apr 14 '06 #9
Lasse Reichstein Nielsen said the following on 4/14/2006 6:54 AM:
Randy Webb <Hi************ @aol.com> writes:
<URL: http://borkweb.com/story/look-ma-cross-domain-scripting >

I think a lot of that misunderstandin g comes, in part, from that
website. It implies that JSON is the secret to it when it isn't. The
secret to it is dynamic script tags.


True. It's the Yahoo web-service format that uses JSON, but what it
sends is not pure JSON, but a function call with an argument that is a
JSON expression. It's good use of JSON, but it's not just that. It could
send any Javascript if it wanted, and it would still work as well.


Yes sir, Yes sir :)

Maybe we need something in the notes that covers just the non-JSON part
of it that can be referred to.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
6880
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... PS i am ready to use JSON as data/object interchange when using AJAX and my J2EE project - because it is easier to traverse the JavaScript object than its XML representation (so of course may argue).
1
1730
by: Red Daly | last post by:
Hello group, I have been using JSON for a while and it has made many things a breeze. However, JSON does not natively describe certain things like pointers and custom types. I created a simple JSON extension that allows cross-references, and I'm asking for your thoughts on a simple type system. I've dubbed this variation RJSON (Red's JSON) for lack of a shorter name. Here's how it works right now:
6
8632
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have tried, including my conclusions/assumptions (which i'll happily be corrected on if it solves my problem!): The requirement not to use a proxy means I can't use the synchronous
6
5481
by: Bart Van der Donck | last post by:
Hello, I'm presenting my new library 'AJAX Cross Domain' - a javascript extension that allows to perform cross-domain AJAX requests. http://www.ajax-cross-domain.com/ Any comments or suggestions are welcome. --
2
2021
by: vunet | last post by:
When implementing JSON as a form of data exchange between server and client, what security measures do I need to consider? For example, I have XMLHttpRequest returning JSON text from the server and eval() converts string to the JavaScript object. I heard about problems with "eval" and idea of using "magic cookies" to avoid attacks. Anyway, what should I consider? Thanks.
16
2979
by: Stevo | last post by:
I'm guessing this is a laughably obvious answer to many here, but it's not to me (and I don't have a server or any knowledge of PHP to be able to try it). It's not strictly a PHP question, but something that PHP guys would know the answer to. I can't think of a more appropriate forum to try. I've heard the ASP and JSP guys aren't as friendly ;-) Let's say we have a HTML page from domain example.com, and that HTML page makes a request to...
6
3988
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
3
6887
Kelicula
by: Kelicula | last post by:
Hi all, I am usually a Perl programmer, I have some background in javascript and am attempting to create a Googleish selector div. Please bear with me, excuse the long introduction... Here's the details: Our site filters content by location. I use zip codes to find radius's. So if a user wants to see content from a location that they don't know the zip code for, I have set up this neat little input field that allows you to type the...
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.