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

时间:2013-09-04 12:14:43

标签: javascript jquery asp.net

大家好,这里是我的函数代码,用于检查我的数据库中是否存在某个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");
                    }
                }
            });


        }

我的下面的webservice代码按预期返回它返回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);
            }

        });
    }

1 个答案:

答案 0 :(得分:0)

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