0

我的 ajax 代码有问题。它应该检查来自 php 的返回值,但它总是返回未定义或其他一些不相关的值。由于我对 ajax 方法很陌生,我似乎无法在这方面找到进展。我在 stackoverflow 和其他相关论坛上搜索了许多关于该解决方案的链接,但没有任何帮助。问题还是一样

这是ajax代码::

$(document).ready(function() {
$('#submit-button').click(function() {
    var path = $('#path').val();

    $.ajax({
        url: 'frontEnd.php',
        data: {path: path },
        type: 'POST',
        dataType: 'json',

        success: function(data) {

            if (data == 1) {
                 alert("Value entered successfully" + data);
           } else if (data == 0) {
                 alert("Sorry an error has occured" + data);
        }

    });

    return false;
})
});

问题在于输出数据的值。如果值成功输入数据库,则 php 代码返回 1,否则返回 0。ajax 片段应该检查返回值并打印适当的消息。但它没有这样做。

这是php代码::

<?php
 require './fileAdd.php';    
$dir_path = $_POST['path'];    
$insVal = new fileAdd($dir_path);
$ret = $insVal->parseDir();
if ($ret ==1 ) {
      echo '1';
   } else {
     echo '0';
    }      
?>

我找不到解决方法。请帮忙;

4

2 回答 2

1
$(document).ready(function() {
$('#submit-button').click(function() {
    var path = $('#path').val();

    $.ajax({
        url: 'frontEnd.php',
        data: {path: path },
        type: 'POST',
        //dataType: 'json', Just comment it out and you will see your data

或者

dataType: 'text',
于 2013-06-30T13:09:16.557 回答
1

因为右}括号不匹配试试这个

$(document).ready(function() {
    $('#submit-button').click(function() {
        var path = $('#path').val();
        $.ajax({
            url: 'frontEnd.php',
            data: {path: path},
            type: 'POST',
            dataType: 'text', //<-- the server is returning text, not json
            success: function(data) {
                if (data == 1) {
                    alert("Value entered successfully" + data);
                } else if (data == 0) {
                    alert("Sorry an error has occured" + data);
                }
            } //<-- you forgot to close the 'success' function
        });
        return false; 
    });
});
于 2013-06-30T13:15:26.477 回答