1

I have a javascript method snippet as below

var  tempUrl = "http://A.com:8081/TestService/serviceMethod";  
jQuery.ajax({  
url:tempUrl,  
type: 'POST',  
data:"getDatareq="+encodedata,  
contentType: 'application/json',  
dataType:'text',  
success:function(result){  
jQuery(".loadingMsg").html("");  
jQuery(".loadingMsg").hide();  
getApptDtls(result);  
},  
complete:function(result){  
jQuery(".loadingMsg").html("");  
jQuery(".loadingMsg").hide();  
jQuery(".popupContent").show();  
jQuery.unblockUI(); 

I am including this method in an html which is hosted in another server with url http://B.com:8081

When i run this html and call this method , the serviceMethod in A.com is not getting hit . What could be the problem here?

Any help is greatly appreciated.

leppie
  • 115,091
  • 17
  • 196
  • 297
NishM
  • 1,706
  • 2
  • 15
  • 26

1 Answers1

1

Use JSONP.

JSONP or "JSON with padding" is a communication technique used in JavaScript. It provides a method to request data from a server in a different domain, something prohibited by typical web browsers because of the same origin policy.

jQuery:

var  tempUrl = "http://A.com:8081/TestService/serviceMethod";  
    $.ajax({
         url:tempUrl,
         dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
         success:function(json){
             // do stuff with json (in this case an array)
             alert("Success");
         },
         error:function(){
             alert("Error");
         },
    });

PHP:

<?php
$arr = array("element1","element2",array("element31","element32"));
$arr['name'] = "response";
echo $_GET['callback']."(".json_encode($arr).");";  // 09/01/12 corrected the statement
?>
Community
  • 1
  • 1
Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102
  • Will the POST work in jSonP . I have tried the same with POST. But its not working. Should i use jSon call back ? – NishM Apr 12 '13 at 17:54