Solved

Not Receiving XML Response From JQuery XML Post

Posted on 2011-03-09
4
552 Views
Last Modified: 2012-05-11
I want to use JQuery AJAX to send the below XML document and receive in response an XML document.  The problem is when I receive the response it comes across as successful but the data is blank.  I am using Javascript JQuery to handle both the request and following response.

XML Document
<?xml version="1.0" encoding="utf-8"?>
	<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<soap:Body>
			<GetRates xmlns="http://www.notavailable.com/notavailable/">
				<objGetRatesRequest>
					<UserId>notavailable</UserId>
					<Password>notavailable</Password>
					<TpaCode>DEMO</TpaCode>
					<DealerNo>DEMO001</DealerNo>
					<NewUsed>U</NewUsed>
					<VIN>WBABN53463PH03667</VIN>
					<VehiclePurchasePrice>24990</VehiclePurchasePrice>
					<VehicleOdometer>19456</VehicleOdometer>
					<SaleDate>2011-05-26T00:00:00</SaleDate>
				</objGetRatesRequest>
			</GetRates>
		</soap:Body>
	</soap:Envelope>

Open in new window


Javascript JQuery Code
$.get('ajax/getRequest.php', function(xmlDoc) { 
				alert(xmlDoc)
				$.ajax({
					url: "https://uat825.notavilable.com/notavailable.webservice/notavailable.asmx",
					processData: false,
					type: "POST",
					data: xmlDoc,
					success: function(data, status) {alert(status);alert(data)}
				});			
			});		

Open in new window

0
Comment
Question by:joshaxton
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 35089647
You can't do cross-domain/protocol ajax call.
It seems it's your case here.
The page used to post or get the data need to be on the same domain and use the same protocol than the page which receive or send the data.

You need to use a proxy, JSONP, or others technics to achieve your goal
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35089748
0
 

Author Comment

by:joshaxton
ID: 35112510
Thanks for your response.  It provided me enough information to create the solution.  Below is the end result snippet of code I am returning to the client.  This works perfectly on a Windows 2008 Server PHP Environment.

<?php
session_start();
include_once "../inc/dbcon.php";
include_once "../inc/globals.php";
include_once "../inc/constants.php";

$VIN= $_REQUEST["VIN"];
$VehicleOdometer= $_REQUEST["VehicleOdometer"];
$NewUsed = $_REQUEST["NewUsed"];
$VehiclePurchasePrice= $_REQUEST["VehiclePurchasePrice"];
$DealerNo= $_REQUEST["DealerNo"];
$SaleDate= date("Y-m-d")."T00:00:00";

$con = getDBConn();
$xmlString = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetRates xmlns="http://www.notavailable.com/notavailable/">
<objGetRatesRequest>
<UserId>notavailable</UserId>
<Password>notavailable</Password>
<TpaCode>notavailable</TpaCode>
<DealerNo>'.$DealerNo.'</DealerNo>
<NewUsed>'.$NewUsed.'</NewUsed>
<VIN>'.$VIN.'</VIN>
<VehiclePurchasePrice>'.$VehiclePurchasePrice.'</VehiclePurchasePrice>
<VehicleOdometer>'.$VehicleOdometer.'</VehicleOdometer>
<SaleDate>'.$SaleDate.'</SaleDate>
</objGetRatesRequest>
</GetRates>
</soap:Body>
</soap:Envelope>';

define('XML_PAYLOAD', $xmlString);
define('XML_POST_URL', 'https://uat825.notavailable.com/notavailable.webservice/notavailable.asmx');

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, XML_POST_URL); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml','Connection: close'));
	
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch); 
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;

if ( curl_errno($ch) ) {
	$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
	$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
	switch($returnCode){
		case 200:
			break;
		default:
			$result = 'HTTP ERROR -> ' . $returnCode;
			break;
	}
}	

curl_close($ch);

echo 'Total time for request: ' . $totalTime . "\n";
echo $result;   

sqlsrv_close($con); 
?>

Open in new window

0
 

Author Closing Comment

by:joshaxton
ID: 35112522
It would've been better if the alternative solutions was presented in detail with coding examples than simply posting a link.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question