javascript - 为什么这个函数不返回 JSON 字符串?

标签 javascript ajax json

function addphoto()
{
    var ajaxRequest = initAjax();
    if (ajaxRequest == false)
    {
        return false;
    }

    // Return Ajax result when the state changes later
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            alert(ajaxRequest.responseText);
            return ajaxRequest.responseText;
        }
    }

    // Capture form elements
    var values = {
        "category" : encodeURIComponent(document.addphoto.category.options[document.addphoto.category.selectedIndex].value),
        "photo_title" : encodeURIComponent(document.addphoto.photo_title.value),
        "photo_descrip" : encodeURIComponent(document.addphoto.photo_descrip.value)
    }

    var queryString = '?', i = 0;
    for (var key in values)
    {
        if (i != 0)
        {
            queryString += '&'
        }
        queryString += key + '=' + values[key];
        i++;
    }

    // Execute Ajax
    ajaxRequest.open("POST", "ajaxcheckform.php" + queryString, true);
    ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxRequest.setRequestHeader("Content-length", queryString.length);
    ajaxRequest.setRequestHeader("Connection", "close");
    ajaxRequest.send(null);
}

function ajaxCheckform(formname)
{
    var response = addphoto(); // <--This is undefined and not sure why
    var responseObj = JSON.parse(response);

    if (responseObj.success == 1)
    {
        // Successful form!
        alert(responseObj.success_text);
    }
    else
    {
        // Error!
        alert(responseObj.error);
    }
}

我确信我一定在某个地方犯了一些基本错误,但我无法找到它。在此脚本中,ajaxCheckform() 是执行几个类似函数之一的函数。上面,我添加了 addphoto(),这是我需要的几个函数之一,如下所示。

顺便说一句,我很想知道我可以动态调用函数。 addphoto() 函数将只是当时被调用的一个这样的函数,我正在尝试找到一种方法来传递 formname 作为我需要的函数。我搜索过 Stackoverflow 和 Google。我没有发现任何有效的方法。

注意,我知道 jQuery,但我还没有做到这一点。我首先需要这个功能才能工作。

最佳答案

这不是addphoto(),而是未定义,而是response未定义。 ajaxRequest 是异步的,addphoto() 函数将在请求完成之前返回。

试试这个

function addphoto() {...

    // Return Ajax result when the state changes later
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            alert(ajaxRequest.responseText);            
            var responseObj = JSON.parse(ajaxRequest.responseText);

            if (responseObj.success == 1) {
                 // Successful form!
                 alert(responseObj.success_text);
            }
           else {
               // Error!
               alert(responseObj.error);
           }        
        }    
    }
....
}

function ajaxCheckform(formname) {
    addphoto();    
}

关于javascript - 为什么这个函数不返回 JSON 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5982769/

相关文章:

javascript - Firefox 有时无法全屏显示完整的图像

php - 当 MySQL 数据库发生变化时,如何使我的网页上的元素自动更新?

ios - 在 WKWebview 和 NSURLRequest 之间共享认证

javascript - 为什么Jquery ajax要给String添加斜杠?

javascript - 如何将代码隐藏信息传递到 JavaScript 函数中

Javascript - 使用 onClick 函数突出显示字符串中的特定文本

javascript - 根据 jQuery 中的另一个链接更改 url

javascript - Ajax上传图片

java - Gson 帮助 - 尝试使用 Gson 获取 JSON 的第一个值(键)

php - Json在php中使用2个foreach获取内容来检索多个节点深度