jquery函数返回true,但无法处理返回值

jquery function returns true but cannot process the return value

本文关键字:处理 返回值 函数 返回 true jquery      更新时间:2023-09-26

嗨,伙计们,这是我为一个函数编写的代码,该函数检查我的数据库中是否存在某个id

function checkifuploaded(citenr) {
        $.ajax({
            type: "POST",
            url: "locationsDB.asmx/checkifexist",
            data: '{"citenr": "' + citenr + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                if (response.d) {
                    return true;
                } else {
                    return false;
                }
                //setinfo();
            },
            failure: function (response) {
                alert(response.d);
            }
        });
    }

现在我有另一个函数,它使用上面的函数

function plotnew(lat, lng) {
            $('#latt').val(lat);
            $('#longt').val(lng);
            $("#Panel2").dialog({
                resizable: false,
                height: 350,
                width: 600,
                modal: true,
                buttons: {
                    "Plot Incident and Save to Database": function () {
                        //$(this).dialog("close");
                        if (checkifuploaded($('#idcr').val())) {
                            $.post(
                                    'insertcrimelocation.aspx',
                                    { lat: $('#latt').val(), long: $('#longt').val(), cirsid: $('#idcr').val() },
                                    function (responseText, textStatus) {
                                        //checkifexist
                                        plot();
                                        $("#Panel2").dialog("close");
                                        $.ambiance({ message: "successfully plotted the incident!",
                                            title: "Success!",
                                            type: "success"
                                        });

                                    })
                                    .error(function () {
                                        $.ambiance({ message: "error on request",
                                            title: "Error!",
                                            type: "error"
                                        });
                                    });
                        } else {
                            $.ambiance({ message: "The C.I.R.S id is not yet uploaded",
                                title: "Error!",
                                type: "error"
                            });
                        }

                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                }
            });

        }

我下面的Web服务代码按预期返回,它返回真实的

<WebMethod()> _
    Public Function checkifexist(citenr As String) As Boolean
        Dim locs As New crimemsapslocationDataContext
        Dim check = locs.OFFENSEs.Where(Function(offense) offense.CITE_NR = citenr).First
        If Not check Is Nothing Then
            Return True
        Else
            Return False
        End If
    End Function

令我惊讶的是,在第二个函数plotnew()上,如果返回值为true,它会以另一种方式执行,而不是通过另一个页面调用函数。我的代码有问题吗?

我让它现在工作代码低于

function checkifuploaded(citenr) {
        $.ajax({
            type: "POST",
            url: "locationsDB.asmx/checkifexist",
            data: '{"citenr": "' + citenr + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                if (response.d) {
                    i squeezed my function here to make it work
                    thanks for all the help and info
                } else {
                    return false;
                }
                //setinfo();
            },
            failure: function (response) {
                alert(response.d);
            }
        });
    }

您可以使用promise对象来处理异步处理。http://api.jquery.com/jQuery.ajax/