PHP proxy ajax doesn't return a response for POST
I am using the following PHP proxy:
//Store the URL
$jURL = $_POST['jURL'];
//Store the POST data
$jData = $_POST['post_data'];
//And let cURL work it's magic.
$ch = curl_init();
//Set the URL
curl_setopt($ch, CURLOPT_URL, $jURL);
//Enable POST data
curl_setopt($ch, CURLOPT_POST, true);
//Us开发者_如何学运维e the $pData array as the POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $jData);
//curl_exec automatically writes the data returned
$response = curl_exec($ch);
curl_close($ch);
echo $response;
and I am calling it by:
var dataString = { 'Contact': Contact, 'address1': address1, 'address2': address2, 'city': city, 'state': state, 'zip': zip, 'Phone1': primPhone, 'Phone2': secPhone, 'email': email,
'key2': key2, 'key5': key5, 'uhsgradyr': uhsgradyr, 'uhighlevel': uhighlevel, 'ucourseint': ucourseint, 'uCampaignID': uCampaignID, 'utextperm':utextperm,
'uleaddate': uleaddate, 'uleadtime':uleadtime};
var postData = {jURL: 'http://test.com/candidate_test.php', postData: dataString};
$.ajax({
type: "POST",
url: "proxy.php",
data: postData,
success: function(data) {
window.location = "thankyou.php";
},
error:function(xhr, ajaxOptions, thrownError){
console.log(xhr.responseText);
}
});
The issue is that why am I still not getting any response back?
Ok, I actually went to the site.. you need to replace some javascript:
$(".Submit").click(function() {
should be
$('#form').submit(function(ev) {
ev.preventDefault();
In the present code, you never say "stop form submission and react to ajax", which is what ev.preventDefault()
does for you.
Old answer: You should set one more option:
curl_setopt(CURLOPT_RETURNTRANSFER, true);
to make sure $response
actually contains the curl's response. See the documentation on curl_exec()
.
That might be because you're setting jData = $_GET['post_data'];
instead of jData = $_POST['post_data'];
In your jQuery .ajax()
call you're setting type: "POST"
so the data will be in $_POST
not $_GET
.
精彩评论