跨域问题与jQuery问题、jQuery

2023-09-10 14:13:27 作者:白鸥掠海

我想访问Web服务在另一个领域,它没有返回值。后来我想通了,这是一个问题bcause的跨域接取。

我在网上搜索,有这么多的文章,但没有一个是新手,像我这样的可读性。 :(

有人可以帮助我如何访问web服务??

下面是我的code。

  //变量添加联系人
VAR addAccountServiceUrl ='http://crm.eyepax.net/organization.asmx?op=WriteOrg'; // preferably写这篇文章从服务器端
VAR OrganizationID = 123;
VAR PARENTID = 123;
VAR AccountManagerID =123;
VAR单位名称=Testapple;
VAR IncorporationNo =23;
VAR地址后=asdfklj asldfj;
VAR CountryID =LK;
VAR VisitAddress =asldkf asldkf asldfas dfasdf;
VAR VisitCountryID =LK;
VAR VisitSwithboard =242344;
VAR VisitFax =234234;
VAR万维网=htt​​p://www.eyepax.com;
VAR活动=真;
VAR RegBy = 345345345345;
无功配置code =28BC9CC3 @ BFEBFBFF0001067A;
VAR标志= 1;
变种LicenceOrganazationID = 1;
VAR SERR;

功能的addContact()
{
//这是即将评论!
警报(调用函数);
//更新web服务soapmesg

VAR的SOAPMessage =
'<肥皂:信封的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema的xmlns:肥皂= http://schemas.xmlsoap.org/soap/envelope/"> \
<肥皂:身体与GT; \
    < WriteOrg的xmlns =htt​​p://eyepax.crm.com/Organization> \
      < OrganizationID>+ OrganizationID +'< / OrganizationID> \
      < PARENTID>+ PARENTID +'< / PARENTID> \
      < AccountManagerID>+ AccountManagerID +'< / AccountManagerID> \
      <单位名称>+单位名称+'< /单位名称> \
      < IncorporationNo>+ IncorporationNo +'< / IncorporationNo> \
      <地址后>'+地址后的+'< /地址后> \
      < CountryID>+ CountryID +'< / CountryID> \
      < VisitAddress>+ VisitAddress +'< / VisitAddress> \
      < VisitCountryID>+ VisitCountryID +'< / VisitCountryID> \
      < VisitSwithboard>+ VisitSwithboard +'< / VisitSwithboard> \
      < VisitFax>+ VisitFax +'< / VisitFax> \
      <万维网>+万维网+'< /网络> \
      <活动>+主动+'< /活动> \
      < RegBy>+ RegBy +'< / RegBy> \
      <结构code基+配置code +'< /配置code取代; \
      <旗>+标志+'< /标志> \
      < LicenceOrganazationID>+ LicenceOrganazationID +'< / LicenceOrganazationID> \
    < / WriteOrg> \
  < / SOAP:身体与GT; \
< /肥皂:信封>';

$阿贾克斯({
网址:addAccountServiceUrl,
键入:POST,
数据类型:XML,
数据:的SOAPMessage,
成功:endAddContact,
错误:函数(jqXHR,textStatus,errorThrown){警报(失败);执行console.log(textStatus);执行console.log(errorThrown);},
的contentType:为text / xml;字符集= \UTF-8 \
});

返回false;
}

功能endAddContact(xmlHtt prequest,状态)
{
    的console.log(xmlHtt prequest);
    执行console.log(状态);
    警报(Web服务叫!);
 $(xmlHtt prequest.responseXML)
    .find('WriteOrgResponse)
    每个(函数()
 {
   。VAR orgres = $(本).find('WriteOrgResult)文本();
   VAR误差= $(本).find('vstrError)文本()。

   警报(orgres +' - '+误差);
 });

 VAR一个= $(xmlHtt prequest.responseXML).find('WriteOrgResult');
 变种B = $(xmlHtt prequest.responseXML).find('vstrError');
 的console.log(一个+ a.text());
 的console.log(b的+ b.text());
}
 

解决方案

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

要使用JSONP请求,你必须修改的dataType 属性 JSONP 。这意味着,但是你不能要求XML,但JSONP而已。

有关JSONP位:

<脚本> 标签绕过跨域限制。这意味着你可以使用标签来从其他服务器上的数据。该标记不支持各种语言,因此不支持XML。

JSONP基本上是JSON,但它周围的函数调用是这样的:

函数名({属性:值})

我可以看到你想知道:那是什么函数名在那里做

这正是使用JSON的差异。由于该功能是包裹着它,您可以用实际的数据!

 <脚本类型=文/ JavaScript的>
VAR函数名=函数(JSON){
    警报(json.property);
}
< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =htt​​p://www.domain.com/jsonp>< / SCRIPT>
 
jquery跨域问题

如果您替换响应内容的第二个脚本标记,它会是行得通的:

 <脚本类型=文/ JavaScript的>
VAR函数名=函数(JSON){
    警报(json.property);
}

函数名({属性:值});
< / SCRIPT>
 

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

约JSONP 另一个线程

I'm trying to access webservice in another domain and it returns nothing. later I figured out it was a issue bcause of cross domain acess.

I searched online and there are so many articles but none is readable by newbie like me. :(

Can someone help me out how to access the webservice??

following is my code.

//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());
}

解决方案

Browsers do not allow cross-domain AJAX calls. Only cross-domain JSONP requests are allowed.

To use JSONP requests, you have to change the dataType property to jsonp. This means however you can not request XML, but JSONP only.

A bit about JSONP:

The <script> tag bypasses the cross-domain limitations. Which means that you can use that tag to get data from other servers. That tag doesn't support all kinds of languages, hence XML is not supported.

JSONP is basically JSON, but with a function call around it like this:

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

I can see you wondering: "What is that functionname doing there?"

That's EXACTLY the difference with JSON. Because the function is wrapped around it, you can use the actual data!

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

If you replace the second script tag with the response content, it'll all make sense:

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

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

Believe it or not, but this minor difference actually enables us to make cross-domain requests much safer.

Another thread about JSONP