我使用ajax将数据(表单形式为html)发送到php,php会处理数据并将结果从php返回给ajax。我检查了php做了它的工作。而且php也得到了正确的结果。
$responseArray = array("type" => "success", "message" => $okMessage);
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
header('Content-Type: application/json; charset=utf8', true);
$encoded = json_encode($responseArray);
echo $encoded;
}
并且实际调用了以下代码,
但是ajax收到的数据是
2017-10-05 23:18:04 CLIENT -> SERVER: EHLO localhost
2017-10-05 23:18:04 SERVER -> CLIENT: 250-smtp.gmail.com at your service, bla...bla...
{"type":"success","message":"Contact form successfully submitted. Thank you, I will get back to you soon!"}
在这种情况下,我只需要最后一行。我试图阻止它打印日志信息。一种解决方案是更改/etc/php.ini。但是我没有这个文件。
这是.js代码片段。
$.ajax({
type: "POST",
url: url,
// dataType:"json", /*can not get data back from php but the output of php is right */
data: $(this).serialize(),
success: function(data) {
// we recieve the type of the message: success x danger and apply it to the
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
// let's compose Bootstrap alert box HTML
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
$('#main-contact-form').find('.messages').html(alertBox);
// empty the form
$('#main-contact-form')[0].reset();
// If we have messageAlert and messageText
if (messageAlert && messageText) {
// inject the alert to .messages div in our form
$('#main-contact-form').find('.messages').html(alertBox);
// empty the form
$('#main-contact-form')[0].reset();
}
}
});
答案 0 :(得分:-1)
php文件
隐藏头部功能(非强制性)$responseArray = array("type" => "success", "message" => $okMessage);
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
//header('Content-Type: application/json; charset=utf8', true);
$encoded = json_encode($responseArray);
echo $encoded;
}
编码的json是正确的。
$.ajax({
type: "POST",
url: url,
dataType:"json",
data: $(this).serialize(),
success: function(data) {
// we recieve the type of the message: success x danger and apply it to the
var messageAlert = 'alert-' + data['type'];
var messageText = data['message'];
// let's compose Bootstrap alert box HTML
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
$('#main-contact-form').find('.messages').html(alertBox);
// empty the form
$('#main-contact-form')[0].reset();
// If we have messageAlert and messageText
if (messageAlert && messageText) {
// inject the alert to .messages div in our form
$('#main-contact-form').find('.messages').html(alertBox);
// empty the form
$('#main-contact-form')[0].reset();
}
}
});
.js代码
数据类型添加了json并使用了数据[&#39; type&#39;]和数据[&#39; message&#39;]
private MediaRecorder myAudioRecorder;
private MediaPlayer mPlayer;
audio.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
initializeRecorder();
recordFile();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
stopRecording();
}
return true;
}
});
private void initializeRecorder() {
File cacheDir = new File(Utils.SD_CARD, Utils.RECORDING_CACHE);
if (!(cacheDir.exists())) {
cacheDir.mkdirs();
}
outputFile = cacheDir + "/" + String.format("%s.m4a", System.currentTimeMillis());
mPlayer = new MediaPlayer();
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//original
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
}
private void recordFile() {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording(){
myAudioRecorder.stop();
myAudioRecorder.release();
}
注意:我没有测试上面的代码。