2012-01-13 17 views
6

別のドメインのwebserviceにアクセスしようとしていますが、何も返されません。後で、私はそれがクロスドメインアクセスの問題であると考えました。Jqueryとのクロスドメインの問題

私はオンラインで検索しましたが、非常に多くの記事がありますが、私のような初心者から読めるものはありません。 :(

誰かが次??

を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="[email protected]"; 
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()); 
} 
+0

これを行うことはできますか? –

答えて

5

ブラウザのみクロスドメインJSONP要求。クロスドメインAJAX呼び出しはできません。 JSONPだけ。

JSONPリクエストを使用するには許可され、あなたがjsonpdataTypeプロパティを変更する必要があります。これは、あなたがXMLを要求することはできませんが意味し​​ますが、されている。


JSONP約ビット:

<script>タグは、クロスドメインの制限をバイパスします。つまり、そのタグを使用して他のサーバーからデータを取得できます。そのタグはすべての種類の言語をサポートしていないため、XMLはサポートされていません。

JSONPは基本的にJSONですが、このようなことの周りの関数呼び出しで:

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

私はあなたが思って見ることができます:「?というのfunctionnameが何をしているのです」

これはJSONとの違いです。関数がラップされているので、実際のデータを使用することができます!

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

レスポンス・コンテンツを持つ2番目のスクリプトタグを交換した場合、それはすべての意味を成します:

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

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

は信じられないかもしれませんが、この小さな違いは、実際にクロスドメインを作成することを可能にはるかに安全です。あなたが外部ドメインに要求を渡すか、パディング別名JSONPでJSONを使用するためにローカルプロキシを使用するか必要なJavascriptを使用してクロスドメイン通信のために

Another thread about JSONP

3

外部サイトでJSONPを使用する可能性がある場合は、その点を参考にしてください。そうでない場合は、Webアプリとリモートサーバーの間のcreating a proxyを調べてください。