Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jquery - Cross Domain Access with PHP Proxy

I'm Accessing a XML (.net) webservice via jQuery and due to cross domain access restrictions I was restricted. I was using Simple PHP proxy and calling the webservice. Being a newbie webdev its really a headache.

I'm geting a funny response as below.

{"status":{"http_code":500},"contents":"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Server was unable to process request. ---&gt; Root element is missing.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>"}

This is my JavaScript

var proxy = 'proxy.php';
//http://crm.eyepax.net/contact.asmx?op=LogIn
//variables for Add Contacts
var addAccountServiceUrl = proxy+'?url='+serviceURL'; // 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;

var userName="test123";
var password="Eyepax321";

var flag="2";

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> 
    <LogIn xmlns="http://eyepax.crm.com/contact"> 
      <UserName>'+userName+'</UserName> 
      <Password>'+password+'</Password> 
      <ConfigurationCode>'+ConfigurationCode+'</ConfigurationCode> 
      <Flag>'+flag+'</Flag> 
    </LogIn> 
  </soap:Body> 
</soap:Envelope>';

$.ajax({
beforeSend: function(jqXHR, settings) {
        //var xhr = new CrossXHR();
       //jqXHR = xhr;
    },
url: addAccountServiceUrl,
type: "POST",
dataType: "xml",
data: soapMessage,
crossDomain: true,
success: endAddAccount,
error: function(jqXHR, textStatus, errorThrown) {
    alert("failure"); 
    console.log(textStatus);
    console.log(errorThrown);},
contentType: "text/xml; charset="utf-8""
});

return false;
}

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


//function for field validation
$("#add_account_page").live('pageinit', function(event) {
//alert("Add Test");    
var account_name_val;
$("#submit").click(function(){
    account_name_val = $("#account_name").val();
    // this function is used to see whether mandatory field Account Name is available
    if(account_name_val==""){
        alert("Please fill Account Name");
        $("#account_name").focus();
    }
    else{
        //calling webservice
            addContact();
            }
        });
   });

PHP proxy source

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was able to send reqeust via PHP proxy and get the result.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...