Solved

Not Receiving XML Response From JQuery XML Post

Posted on 2011-03-09
4
546 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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:joshaxton
Comment Utility
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
Comment Utility
It would've been better if the alternative solutions was presented in detail with coding examples than simply posting a link.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now