2012-01-13 40 views
6

Tôi đang cố gắng truy cập webservice trong một tên miền khác và không trả lại gì. sau đó tôi đã tìm ra nó là một vấn đề bcause của acess miền chéo.vấn đề tên miền chéo với Jquery

Tôi đã tìm kiếm trực tuyến và có rất nhiều bài viết nhưng không ai đọc được những người mới như tôi. :(

Ai đó có thể giúp tôi hiểu làm thế nào để truy cập webservice ??

Sau đây là mã của tôi.

//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

bạn có thể thực hiện việc này không? –

Trả lời

5

Trình duyệt không cho phép cross-domain AJAX gọi. Chỉ cross-domain JSONP yêu cầu được phép.

để sử dụng yêu cầu JSONP, bạn phải thay đổi dataType tài sản để jsonp. Điều này có nghĩa tuy nhiên bạn không thể yêu cầu XML, nhưng

chỉ JSONP.

Một chút thông tin về JSONP:

Thẻ <script> bỏ qua những hạn chế cross-domain. Điều đó có nghĩa là bạn có thể sử dụng thẻ đó để lấy dữ liệu từ các máy chủ khác. Thẻ đó không hỗ trợ tất cả các loại ngôn ngữ, do đó XML không được hỗ trợ.

JSONP về cơ bản là JSON, nhưng với một cuộc gọi chức năng xung quanh nó như thế này:

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

tôi có thể thấy bạn tự hỏi: "được functionName rằng làm gì có"

Đó chính xác là sự khác biệt với JSON. Bởi vì hàm được bao quanh nó, bạn có thể sử dụng dữ liệu thực tế!

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

Nếu bạn thay thế thẻ kịch bản thứ hai với nội dung trả lời, nó sẽ làm cho tinh thần tất cả:

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

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

Tin hay không, nhưng sự khác biệt nhỏ này thực sự cho phép chúng tôi thực hiện cross-domain yêu cầu an toàn hơn nhiều.

Another thread about JSONP

3

Đối với truyền thông miền chéo sử dụng Javascript bạn cần phải hoặc sử dụng một proxy địa phương để vượt qua các yêu cầu đối với các lĩnh vực bên ngoài hoặc sử dụng JSON với đệm aka JSONP.

Nếu trang web bên ngoài cung cấp khả năng sử dụng JSONP, hãy thực hiện điều đó. Nếu không, hãy xem creating a proxy giữa ứng dụng web của bạn và máy chủ từ xa.

Các vấn đề liên quan