PHP将JSON返回JQUERY AJAX CALL [英] PHP returning JSON to JQUERY AJAX CALL

查看:92
本文介绍了PHP将JSON返回JQUERY AJAX CALL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在努力摆脱JQUERY,AJAX和PHP的束缚.

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

我现在可以调用PHP OK,处理表单元素并发送电子邮件,但是我不处理返回AJAX的问题.我一直在激活error:选择器,当我尝试列出返回的JSON时,我得到了信息,这显然是错误的.

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.

具有假定的JSON返回值的PHP

PHP with supposed JSON return

<?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和AJAX

Javascript and 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();
};

列出假定的JSON数据

Listing of supposed JSON data

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.

推荐答案

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

You can return json in PHP this way:

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

这篇关于PHP将JSON返回JQUERY AJAX CALL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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