获取parseerror同时使用$就收到json_en code响应 [英] Getting parseerror while receiving response from json_encode using $.ajax

查看:115
本文介绍了获取parseerror同时使用$就收到json_en code响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想试试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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆