[英]Pass php array to Ajax variable
i need to pass an array variable to Ajax request 我需要将数组变量传递给Ajax请求
<?
$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: 我需要将$ postData传递给ajax变量数据:
$(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. 我成功获得了$ postData。 All the code is in one php page. 所有代码都在一个php页面中。
Defining $postData
like : 像这样定义$postData
:
<?php $postData = json_encode(array('FirstName'=>$_POST['user_name'])); ?>
You can send directly the Json without datatype
like : 您可以直接发送不带datatype
的Json,例如:
$.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 : 或者,如果您必须使用dataType: 'json'
和ContentType选项(对于服务器端很重要),则可以在发送ajax之前解析json,例如:
$.ajax({
url: "http://XXXXXXXXX/api/CallBackForm",
type: "POST",
crossDomain: true,
data: JSON.parse('<?php echo $postData; ?>'),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.