PHP 将 JSON 返回给 JQUERY AJAX CALL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7064391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:56:07  来源:igfitidea点击:

PHP returning JSON to JQUERY AJAX CALL

phpjsonjquery

提问by mcl

I am still struggling to get my head around the ins and out of JQUERY, AJAX and PHP.

我仍在努力了解 JQUERY、AJAX 和 PHP 的来龙去脉。

I can now call the PHP OK, process the form elements and send an email, but I am not handling the return to the AJAX. I am always getting the error:selector activated and when I try to list the supposed JSON returned, I get info, that is obviously wrong.

我现在可以调用 PHP OK,处理表单元素并发送电子邮件,但我没有处理对 AJAX 的返回。我总是error:激活选择器,当我尝试列出返回的假定 JSON 时,我得到信息,这显然是错误的。

PHP with supposed JSON return

PHP 与假定的 JSON 返回

<?php

touch('phpTouch.txt');
// process email
$email=1;
if ($email) {
    $value = array('return' => 1, 'msg1' => 'Message sent OK, we will be in touch ASAP');
} else {
    $value = array('return' => 0, 'msg1' => 'Message Failed, please try later');
}
$output = $json->encode($value);
echo $output;

?>

Javascript and AJAX

Javascript 和 AJAX

function submitForm(evt) {
    $('#msgid').html('<h1>Submitting Form (External Routine)</h1>');
    if ($('#formEnquiry').valid() ) {
        $("#msgid").append("<h1>(Outside Ready) VALIDATED send to PHP</h1>");
            $.ajax({
            url: "ContactFormProcess3.php",
            type: "POST",
            data: $('#formEnquiry').serialize(),
            dataType: "json",
            success: function (data) {
                alert("SUCCESS:");
                for(var key in data) {
                    $('#msgid').append(key);
                    $('#msgid').append('=' + data[key] + '<br />');
                }
            },
            error: function (data) {
                alert("ERROR: ");
                for(var key in data) {
                    $('#msgid').append(key);
                    $('#msgid').append('=' + data[key] + '<br />');
                }
            }
        });
    } else {
        $('#msgid').append('<h1>(Outside Ready) NOT VALIDATED</h1>');
    }
    evt.preventDefault();
};

Listing of supposed JSON data

假设的 JSON 数据列表

readyState=4
setRequestHeader=function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this}
getAllResponseHeaders=function (){return s===2?n:null}
getResponseHeader=function (a){var c;if(s===2){if(!o){o={};while(c=bF.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c}
overrideMimeType=function (a){s||(d.mimeType=a);return this}
etc etc 

If anyone can advise as to what stupid mistake I have made, then I would be most grateful.

如果有人能告诉我我犯了什么愚蠢的错误,我将不胜感激。

回答by Arnaud Le Blanc

You can return json in PHP this way:

您可以通过这种方式在 PHP 中返回 json:

header('Content-Type: application/json');
echo json_encode(array('foo' => 'bar'));
exit;