Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ajax成功函数不能返回值_Php_Jquery_Ajax - Fatal编程技术网

Php Ajax成功函数不能返回值

Php Ajax成功函数不能返回值,php,jquery,ajax,Php,Jquery,Ajax,我使用jQueryajax函数来获取数据库值code和subcode。每当我获取代码和subcode时,我都必须获取存储在另一个表中与code和subcode对应的名称。 我使用的第一个函数是 function loadSecretaries() { var items = ""; $.getJSON("Tac_members_GetTacSecretaries.php", function(data) { $.each(data, function(index

我使用jQuery
ajax
函数来获取数据库值code和subcode。每当我获取代码和subcode时,我都必须获取存储在另一个表中与code和subcode对应的名称。 我使用的第一个函数是

function loadSecretaries()
{
    var items = "";
    $.getJSON("Tac_members_GetTacSecretaries.php", function(data) {

        $.each(data, function(index, item) {
            var name = getSecretariesName(item.ECODE, item.ECODE_S);
            items += "<option value='" + item.ECODE + "'>" + item.ECODE_S + </option>";    
        });
        $("#select_reviewer_secretary").html(items);
    });

}
当第一个函数中出现警报“name”时,我得到了未定义的
。如何解决这个问题。提前谢谢。

试试这个

   function getSecretariesName(code, subcode)
{

    var items = "";
    var name = "";
    $.ajax({
        async: false,
        url: '../PhpProject1/Tac_Members_GetReviewerNames.php',
        dataType: 'json',
        type: 'POST',
        data: {"code": code, "subcode": subcode},
        success: function(response) {
            alert(response.ename) ;
           name = response.ename;
        //here i want to return ename to first function
        },
        error: function(x, e) {
            if (x.status === 0) {
                alert('You are offline!!\n Please Check Your Network.');
            } else if (x.status === 404) {
                alert('Requested URL not found.');
            } else if (x.status === 500) {
                alert('Internel Server Error - Please try after relogin to the application');
            } else if (e === 'parsererror') {
                alert('Parsing JSON Request failed.');
            } else if (e === 'timeout') {
                alert('Request Time out.');
            } else {
                alert('Unknow Error.\n' + x.responseText);
            }
        }

    });
return name;
}

从开发工具中的ajax调用中,您的响应是什么样子的?我得到了代码和子代码。但我没有得到名称。我正在尝试将名称加载到select选项中,而不是代码和子代码。因此,您调用的ajax端点听起来像是有问题-如果没有返回属性,然后,您应该查看端点的文档,或者,如果您负责该端点,请调试端点。您可以向我们展示您的php函数吗?我在response.ename中获得了名称。但是我无法将其返回到第一个函数。我已经尝试过此操作。但是..但是?成功了吗?没有。同样的问题也存在。这应该行得通。现在我已经更改了name变量的范围。在name变量的作用域被限制为success函数之前,这就是您无法返回的原因。@sumeet这不起作用,因为ajax调用是异步的。。。这意味着在调用ajax调用的success函数之前,
getSecretariesName
将返回。
   function getSecretariesName(code, subcode)
{

    var items = "";
    var name = "";
    $.ajax({
        async: false,
        url: '../PhpProject1/Tac_Members_GetReviewerNames.php',
        dataType: 'json',
        type: 'POST',
        data: {"code": code, "subcode": subcode},
        success: function(response) {
            alert(response.ename) ;
           name = response.ename;
        //here i want to return ename to first function
        },
        error: function(x, e) {
            if (x.status === 0) {
                alert('You are offline!!\n Please Check Your Network.');
            } else if (x.status === 404) {
                alert('Requested URL not found.');
            } else if (x.status === 500) {
                alert('Internel Server Error - Please try after relogin to the application');
            } else if (e === 'parsererror') {
                alert('Parsing JSON Request failed.');
            } else if (e === 'timeout') {
                alert('Request Time out.');
            } else {
                alert('Unknow Error.\n' + x.responseText);
            }
        }

    });
return name;
}