source

PHP가 JSON을 JQUERY AJAX 호출로 반환

itover 2023. 3. 28. 21:42
반응형

PHP가 JSON을 JQUERY AJAX 호출로 반환

JQUERY, AJAX, PHP에 대한 자세한 내용은 아직 파악하지 못하고 있습니다.

이제 PHP OK를 호출하여 폼 요소를 처리하고 이메일을 보낼 수 있지만 AJAX로의 반환은 처리하지 않습니다.나는 항상 그 일을 해내고 있다.error:셀렉터가 활성화되어 있다고 생각되는 JSON이 반환되었다고 생각되는 것을 표시하려고 하면 정보를 얻을 수 있는데, 이는 명백히 잘못된 것입니다.

JSON 반환이 상정된 PHP

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

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 데이터 목록

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 

제가 어떤 어리석은 실수를 저질렀는지 조언해주시면 감사하겠습니다.

다음과 같이 PHP에서 json을 반환할 수 있습니다.

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

언급URL : https://stackoverflow.com/questions/7064391/php-returning-json-to-jquery-ajax-call

반응형