0

大家好,这是我的代码,用于检查我的数据库中是否存在某个 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 服务代码按预期返回它返回 true

<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);
            }

        });
    }
4

1 回答 1

0

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

于 2013-09-04T12:26:38.347 回答