简体   繁体   English

在另一台服务器中访问REST服务

[英]Accessing REST Service in another server

I have a javascript method snippet as below 我有一个如下的javascript方法片段

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 我在url为http://B.com:8081的另一台服务器中托管的html中包含此方法

When i run this html and call this method , the serviceMethod in A.com is not getting hit . 当我运行此html并调用此方法时,A.com中的serviceMethod并未受到攻击。 What could be the problem here? 这可能是什么问题?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Use JSONP . 使用JSONP

JSONP or "JSON with padding" is a communication technique used in JavaScript. JSONP或“带填充的JSON”是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. 它提供了一种从不同域中的服务器请求数据的方法,由于相同的原始策略,典型的Web浏览器禁止这样做。

jQuery: 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:

<?php
$arr = array("element1","element2",array("element31","element32"));
$arr['name'] = "response";
echo $_GET['callback']."(".json_encode($arr).");";  // 09/01/12 corrected the statement
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM