Cross-domain issue with jQuery

I am trying to access the webservice in another domain and returning nothing. I later realized that this is a cross-power issue.

I searched on the Internet, and there are so many articles, but no one is read by a novice like me. :(

Can someone help me how to access the web service?

Below 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()); } 
+6
source share
2 answers

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

To use JSONP requests, you must change the dataType property to jsonp . This means that you cannot request XML, but only JSONP.


A bit about JSONP:

The <script> bypasses restrictions between domains. This means that you can use this tag to receive data from other servers. This tag does not support all types of languages, therefore XML is not supported.

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

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

I see that you are wondering: "What is this function name doing there?"

This is EXACTLY the difference with JSON. Since 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 content of the response, all of this will make sense:

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

Believe it or not, this slight difference actually allows us to make cross-domain requests much safer.

Another thread about JSONP

+5
source

For cross-domain communication using Javascript, you need to either use a local proxy to send requests to external domains, or use JSON with the aka JSONP add-on.

If the external site offers the ability to use JSONP, go with it. If not, consider creating a proxy between your web application and the remote server.

+3
source

Source: https://habr.com/ru/post/905893/


All Articles