【问题标题】:cross domain issue with JqueryJquery的跨域问题
【发布时间】:2012-02-09 11:57:42
【问题描述】:

我正在尝试访问另一个域中的 Web 服务,但它没有返回任何内容。后来我发现这是跨域访问的问题。

我在网上搜索了很多文章,但没有一个像我这样的新手可以阅读。 :(

谁能帮我看看如何访问网络服务?

以下是我的代码。

//variables for Add Contacts
var addAccountServiceUrl = 'http://crm.eyepax.net/organization.asmx?op=WriteOrg'; // Preferably write this out from server side
var OrganizationID=123;
var ParentID=123    ;
var AccountManagerID="123";
var OrganizationName="Testapple";
var IncorporationNo="23";
var PostAddress="asdfklj asldfj";
var CountryID="LK";
var VisitAddress="asldkf asldkf asldfas dfasdf";
var VisitCountryID="LK";
var VisitSwithboard="242344";
var VisitFax="234234";
var Www="http://www.eyepax.com";
var Active=true;
var RegBy=345345345345;
var ConfigurationCode="28BC9CC3@BFEBFBFF0001067A";
var Flag=1;
var LicenceOrganazationID=1;
var sErr;

function addContact()
{
//this is to be commented soon! 
alert("function called");
//update the webservice soapmesg

var soapMessage =
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \
<soap:Body> \
    <WriteOrg xmlns="http://eyepax.crm.com/Organization"> \
      <OrganizationID>'+OrganizationID+'</OrganizationID> \
      <ParentID>'+ParentID+'</ParentID> \
      <AccountManagerID>'+AccountManagerID+'</AccountManagerID> \
      <OrganizationName>'+OrganizationName+'</OrganizationName> \
      <IncorporationNo>'+IncorporationNo+'</IncorporationNo> \
      <PostAddress>'+PostAddress+'</PostAddress> \
      <CountryID>'+CountryID+'</CountryID> \
      <VisitAddress>'+VisitAddress+'</VisitAddress> \
      <VisitCountryID>'+VisitCountryID+'</VisitCountryID> \
      <VisitSwithboard>'+VisitSwithboard+'</VisitSwithboard> \
      <VisitFax>'+VisitFax+'</VisitFax> \
      <Www>'+Www+'</Www> \
      <Active>'+Active+'</Active> \
      <RegBy>'+RegBy+'</RegBy> \
      <ConfigurationCode>'+ConfigurationCode+'</ConfigurationCode> \
      <Flag>'+Flag+'</Flag> \
      <LicenceOrganazationID>'+LicenceOrganazationID+'</LicenceOrganazationID> \
    </WriteOrg> \
  </soap:Body> \
</soap:Envelope>';

$.ajax({
url: addAccountServiceUrl,
type: "POST",
dataType: "xml",
data: soapMessage,
success: endAddContact,
error: function(jqXHR, textStatus, errorThrown) {alert("failure"); console.log(textStatus);console.log(errorThrown);},
contentType: "text/xml; charset=\"utf-8\""
});

return false;
}

function endAddContact(xmlHttpRequest, status)
{
    console.log(xmlHttpRequest);
    console.log(status);
    alert("webservice called!");
 $(xmlHttpRequest.responseXML)
    .find('WriteOrgResponse')
    .each(function()
 {
   var orgres = $(this).find('WriteOrgResult').text();
   var error = $(this).find('vstrError').text();

   alert(orgres +' -'+ error);
 });

 var a = $(xmlHttpRequest.responseXML).find('WriteOrgResult');
 var b = $(xmlHttpRequest.responseXML).find('vstrError');
 console.log("a"+a.text());
 console.log("b"+b.text());
}

【问题讨论】:

  • 你能做到吗?

标签: jquery xml ajax cross-domain


【解决方案1】:

浏览器不允许跨域 AJAX 调用。只允许跨域JSONP 请求。

要使用 JSONP 请求,您必须将 dataType 属性更改为 jsonp。这意味着您不能请求 XML,只能请求 JSONP。


一点关于 JSONP:

&lt;script&gt; 标签绕过了跨域限制。这意味着您可以使用该标签从其他服务器获取数据。该标签不支持所有类型的语言,因此不支持 XML。

JSONP 基本上是 JSON,但它周围有一个函数调用,如下所示:

functionname({"property":"value"})

我可以看到您想知道:“那个函数名在那里做什么?”

这正是与 JSON 的区别。因为函数是包裹在它周围的,所以你可以使用实际的数据!

<script type="text/javascript">
var functionname = function(json) {
    alert(json.property);
}
</script>
<script type="text/javascript" src="http://www.domain.com/jsonp"></script>

如果你用响应内容替换第二个脚本标签,一切都会有意义:

<script type="text/javascript">
var functionname = function(json) {
    alert(json.property);
}

functionname({"property":"value"});
</script>

信不信由你,但这个微小的差异实际上使我们能够使跨域请求更加安全。

Another thread about JSONP

【讨论】:

    【解决方案2】:

    对于使用 Javascript 的跨域通信,您需要使用本地代理将请求传递到外部域,或者使用带有填充的 JSON,即 JSONP

    如果外部站点提供了使用 JSONP 的可能性,那就去吧。如果没有,请查看您的 Web 应用和远程服务器之间的 creating a proxy

    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多