[英]get data from php using ajax
I have the following ajax function: 我有以下ajax函数:
var data=event.target.result;
var fileName=encodeURIComponent('audio_recording_' + new Date().getMinutes() + '.wav');
$.ajax({
type: 'POST',
url: 'readFile.php',
data: {"fileName":fileName,"data":data},
success: function(data){
console.log(data);
}
});
the server-side code 服务器端代码
<?php
$fileName=$_POST["fileName"];
$data=$_POST["data"];
$dh = opendir('upload/');
$contents = file_get_contents('C:/wamp/www/JSSoundRecorder/upload/'.$fileName);
// echo $contents;
echo $fileName;
echo $data;
In the console.log(data) i'm obtaining the correct results (filename and data) what I want is to have each info alone to use later. 在console.log(data)中,我正在获取正确的结果(文件名和数据),我想要的是让每个信息单独供以后使用。 that is fileName in a variable and data in another variable in the success function so that I can use them later in the program.I think I should use maybe localstorage and json.stringify??is that correct.or is there another way.and if that is true can you help me how to use localstorage here? 那是成功函数中变量中的fileName以及成功函数中另一个变量中的数据,以便我以后可以在程序中使用它们。我认为我应该使用localstorage和json.stringify吗?那是正确的。如果是这样,您能帮我在这里使用本地存储吗? thank you in advance 先感谢您
Try this 尝试这个
var data=event.target.result;
var fileName=encodeURIComponent('audio_recording_' + new Date().getMinutes() + '.wav');
$.ajax({
type: 'POST',
url: 'readFile.php',
data: {"fileName":fileName,"data":data},
dataType: 'json', //<==== Add this
success: function(data){
console.log(data.filename);
console.log(data.data);
}
}); });
and you php should be: 而您的php应该是:
<?php
$fileName=$_POST["fileName"];
$data=$_POST["data"];
$dh = opendir('upload/');
$contents = file_get_contents('C:/wamp/www/JSSoundRecorder/upload/'.$fileName);
$data = array('filename'=>$filename, 'data'=>$data);
echo json_encode($data);
?>
You can put the info in an object or an associative array an then use json_encode, that converts the object in a serialized string. 您可以将信息放入对象或关联数组中,然后使用json_encode将对象转换为序列化字符串。 Then you can parse the JSON string in Javascript: 然后,您可以解析Javascript中的JSON字符串:
In php, if you have an object or assocciative array, you will write: 在php中,如果您有对象或关联数组,您将编写:
echo json_encode($yourObjectOrArray);
Supose data is the data you receive in the success function: 假设数据是您在成功函数中收到的数据:
var data = '{"fileName":"yourFileName","data": "yourData", "moreData" : "moreData"}';
var obj = JSON.parse(json);
try 尝试
php PHP
$result = array("filename" => $fileName, "data" => $data);
echo json_encode($result);
jquery jQuery的
success: function(response){
console.log(response);
// var myObject = $.parseJSON(response)
// here you have an object with all values now
}
myObject is needed, depending on if you use dataType: "JSON"
in your ajax or not. 需要myObject,具体取决于您是否在ajax中使用dataType: "JSON"
。
You should use JSON in javascript part to have it as an object. 您应该在javascript部分中使用JSON,以将其作为对象。
You can use var jsonData = JSON.parse(data.data)
您可以使用var jsonData = JSON.parse(data.data)
On the server you should encode it in json with the corresponding PHP function: json_encode(YOUR_ARRAY) 在服务器上,您应该使用相应的PHP函数将其编码为json:json_encode(YOUR_ARRAY)
More information here: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/JSON/parse 此处的更多信息: https : //developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/JSON/parse
There is a great example here: http://www.caveofprogramming.com/frontpage/articles/php/php-json-an-example-javascript-json-client-with-php-server/ 这里有一个很好的例子: http : //www.caveofprogramming.com/frontpage/articles/php/php-json-an-example-javascript-json-client-with-php-server/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.