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
For cross domain communication using Javascript you need to either use a local proxy to pass the requests to external domains or use JSON with padding aka JSONP.
If the external site offers the possibility to use JSONP, go with that. If not, look into creating a proxy between your Web app and the remote server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With