Reputation: 477
i need to pass an array variable to Ajax request
<?
$postData = array(
'FirstName'=>$_POST['user_name'],
'Telephone'=>$_POST['user_phone'],
'Description' =>$_POST['komment'],
'Classifierid'=>'5E0696FD-831E-E611-9426-005056BAB261'
);
$postData = json_encode($postData);?>
i need to pass $postData to ajax variable data:
$(document).ready(function () {
var postData=<?php $postData ?>;
$.ajax({
url: "http://XXXXXXXXX/api/CallBackForm",
type: "POST",
crossDomain: true,
data: I need to put $posData here,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
});
});
`
I get the $postData successfully. All the code is in one php page.
Upvotes: 0
Views: 4481
Reputation: 2748
Defining $postData
like :
<?php $postData = json_encode(array('FirstName'=>$_POST['user_name'])); ?>
You can send directly the Json without datatype
like :
$.ajax({
url: "http://XXXXXXXXX/api/CallBackForm",
type: "POST",
crossDomain: true,
data: '<?php echo $postData; ?>',
});
Or if you have to use the dataType: 'json'
and ContentType option (important for server side), you can parse the json before sending ajax like :
$.ajax({
url: "http://XXXXXXXXX/api/CallBackForm",
type: "POST",
crossDomain: true,
data: JSON.parse('<?php echo $postData; ?>'),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
});
Upvotes: 3