获取parseerror同时使用$就收到json_en code响应 [英] Getting parseerror while receiving response from json_encode using $.ajax
问题描述
我想试试pretty的简单的请求,并通过使用$就得到响应的事情。 JSON。我曾尝试了很多从PHP文件返回一个简单的文本短语。 我的JS code部分是这样的,它调用beemailer.php得到响应。
I'm trying to try a pretty simple request and get response thing using $.ajax via. json. I have tried alot to return a simple text phrase from a php file. My js part of code goes like this that calls a beemailer.php to get response.
$.ajax({
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
url: 'beemailer.php',
data: {
'name': data_name,
'email': data_email,
'message': data_message,
'subject': data_subject
},
success: function (data) {
var output = jQuery.parseJSON(data);
console.log(output);
alert(output);
},
error: function (error, x, y) {
console.log(error);
alert(x, y);
}
)
};
和beemailer.php是这样的:
And beemailer.php is like this:
<?php
$to = "ashish_sharma307@hotmail.com";
$subject = $_POST['subject'];
$message =$_POST['message'];
$headers = "From:".$_POST['name']."(".$_POST['email'].")";
mail($to,$subject,$message,$headers);
$response = "The mail has been sent. Thank You the valid mail.";
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($response);
?>
我需要的,现在仅仅是为了拿到$响应文本得到通知。 任何帮助将AP preciated。
What I need for now is simply to get that $response text get alerted. Any help will be appreciated.
推荐答案
从 AJAX
删除低于code呼叫,并尝试:
Remove below code from ajax
call and try:
contentType: "application/json; charset=utf-8",
jQuery.parseJSON(数据)
不需要为内容类型
设置和连接codeD在 JSON
通过以下code:
jQuery.parseJSON(data)
is not needed as Content-Type
is set and encoded in json
by below code:
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($response);
更新code测试数据:
Updated code with test data:
JS
$.ajax({
type: 'POST',
dataType: "json",
url : 'beemailer.php',
data: {name: 'rai', email: 'rai@test.com', message: 'Test message', subject: 'Test subject'},
success : function(data) {
console.log(data);
alert(data);
},
error: function(error, x, y)
{
console.log(error);
alert(x, y);
}
});
PHP(beemailer.php)
PHP (beemailer.php)
<?php
$to = "ashish_sharma307@hotmail.com";
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From:" . $_POST['name'] . "(" . $_POST['email'] . ")";
mail($to, $subject, $message, $headers);
$response = "The mail has been sent. Thank You the valid mail.";
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($response);
?>
对象的示例:
Example of object:
$result = array();
$result['status'] = 'success';
$result['message'] = 'The mail has been sent. Thank You the valid mail.';
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($result);
JS访问对象:
JS to access object:
$.ajax({
type: 'POST',
dataType: "json",
url : 'beemailer.php',
data: {name: 'rai', email: 'rai@test.com', message: 'Test message', subject: 'Test subject'},
success : function(data) {
alert('status: ' + data.status + '\nmessage: ' + data.message);
},
error: function(error, x, y)
{
console.log(error);
alert(x, y);
}
});
这篇关于获取parseerror同时使用$就收到json_en code响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!