• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Jquery Ajax Cross Domains using JSONP (getJSON) ...

If you are using Jquery Ajax to call to a remote url, your code will maybe generate an error like this “Access to restricted URI denied” code: “1012″ or “Permission Denied …”

To solve this problem, Jquery now supports JSONP natively, you will load JSON from a remote url then an extra callback will be provided for the server to interpret.

JSONP is a technique that allows you to transfer JSON data across multiple domains.

Below is a very simple demo illustrate how to call to a remote url using Jquery Ajax Jsonp:

HTML Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>rapid-DEV.net Jquery Ajax JSON Cross Domain Example</title>
</head>
<script>

 jQuery(document).ready(function(){

  jQuery("#btn").click(function(){

   var dataString = "Name=" + jQuery("#txtName").val() + "&&Message=" + jQuery("#txtMessage").val();
      var url = "http://rapid-dev.net/demo/json_process.php?" + dataString;

    $.getJSON(url + "&jsoncallback=?", function(data){
    Name = data.Name;
    Message = data.Message;

    if(Name != "" && Message != "")
    {
     jQuery("#td_msg").html("Hello " + Name + ", you typed: " + Message);
    }
    else
    {
     jQuery("#td_msg").html("Please enter Name and Message to get the result!!!");
    }

   })

   return false;

  });
 });
</script>
<body>
<table cellspacing="0" cellpadding="2" border="0" width="30%" align="center">
<tbody>
 <tr><td colspan="2" style="color:red" id="td_msg"></td></tr>
 <tr><td colspan="2"><h3>Jquery Ajax JSON Cross Domain Example</h3></td></tr>
 <tr>
  <td align="left">Your name: </td>
  <td align="left"><input type="text" name="txtName" id="txtName"/></td>
 </tr>
 <tr>
  <td align="left">Type something: </td>
  <td align="left"><input type="text" name="txtMessage" id="txtMessage" /></td>
 </tr>
 <tr>
  <td></td>
  <td align="left"><input type="button" id="btn" name="btn" value="Submit" /></td>
 </tr>
</tbody>
</table>
</body>
</html>

PHP Process Page:

<?php
 $Name = $_GET["Name"];
 $Message = $_GET["Message"];

 echo $_GET["jsoncallback"] . "({"Name": "" . $Name . "", "Message": "" . $Message . ""})";
?>

Click here to view online demo.


Download the source code: rd-jquery-ajax-json-cross-domains

 Original Source:
http://rapid-dev.net/2009/04/jquery-ajax-cross-domains-using-jsonp-getjson/

AddThis Social Bookmark Button

Posted at 08:56:05 am | Permalink | Posted in JSON  jQuery  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (89)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (79)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (171)
  • JSON (70)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (224)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange