简体   繁体   English

如何从jQuery ajax调用javascript函数?

[英]how to call javascript function from jQuery ajax?

I have jQuery ajax call to get results from the server, and in success, the code should call javascript function which is not in jQuery region code, so I got error in firebug: that the call of function doesnt have reference. 我有jQuery ajax调用来从服务器获取结果,并且成功地,代码应调用不在jQuery区域代码中的javascript函数,因此我在firebug中出错:该函数的调用没有引用。

here is my code (see the ajax call in function addGMarker): 这是我的代码(请参见函数addGMarker中的ajax调用):

function test1234(res) {
    PreInfo = res;

    popupContentHTML = deviceMoreinfo_callBack_ForGoogle(PreInfo);

    var sum = '<p>Please, Select <b>[Sensors Reading List]</b> tab to view vehcile sensors reading, and select <b>[Device Communication Commands]</b> tab to send commands for the device:</p><br/>';
    var tabs = [
new MaxContentTab('Sensors Reading List', maxContentDiv),
new MaxContentTab('Device Communication Commands', maxContentDivForCommands)];

    this.openMaxContentTabsHtml(map, popupContentHTML, sum, tabs, { maxTitle: "Sensors and Features" });

    var iw = map.getTabbedMaxContent();
    iw.id = this.id;

    GEvent.addListener(iw, 'selecttab', function (tab) {
        var node = tab.getContentNode();

        switch (tab.getLabel()) {
            case 'Sensors Reading List':
                maxContentDiv.innerHTML = '<IMG SRC="../../../images/FullModeIcons/LoadingImage.gif" /> Loading...';
                //GetSensorsReading(this.id, ClientID, "En", GetSensorsReading_CallBack);
                jQuery.ajax({
                    type: "POST",
                    url: "../../../DevicesManagerAjax.asmx/GetSensorsReading",
                    data: "{Device_ID: '" + this.id + "', ClientID: '" + ClientID + "', Page_ID: 'En'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    timeout: 70000,
                    success: function (msg) {
                        var res = msg.d;
                        GetSensorsReading_CallBack(res);
                    },
                    error: function (xhr, status, errorThrown) {
                        alert("An error occered, " + errorThrown);
                    }
                });
                break;
            case 'Device Communication Commands':
                maxContentDivForCommands.innerHTML = '<IMG SRC="../../../images/FullModeIcons/LoadingImage.gif" /> Loading...';
                //GetContorolableSensors(this.id, ClientID, "En", GetContorolableSensors_CallBack);
                jQuery.ajax({
                    type: "POST",
                    url: "../../../DevicesManagerAjax.asmx/GetContorolableSensors",
                    data: "{Device_ID: '" + this.id + "', ClientID: '" + ClientID + "', Page_ID: 'En'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    timeout: 70000,
                    success: function (msg) {
                        var res = msg.d;
                        GetContorolableSensors_CallBack(res);
                    },
                    error: function (xhr, status, errorThrown) {
                        alert("An error occered, " + errorThrown);
                    }
                });
                break;
        }
    });
}


function addGMarker(ID, point, Marker_Icon) {
    icon = new GIcon(G_DEFAULT_ICON);

    if (_IconClientID == "0") {
        if (Marker_Icon == "blue") {
            if (ID == FollowVechicleID) {
                icon.image = "../../Images/Icons/" + _Follow_Icon;
            }
            else {

                icon.image = "../../Images/Icons/" + _Normal_Icon;
            }

            if (ID == FollowVechicleID) {
                ShowLastThreePositions();
            }
        }
        else {

            icon.image = "../../Images/Icons/" + _Speed_Icon;
        }
    }
    else {
        if (Marker_Icon == "blue") {
            if (ID == FollowVechicleID) {

                icon.image = "../../Images/Icons/ClientsIcons/" + _Follow_Icon;
            }
            else {

                icon.image = "../../Images/Icons/ClientsIcons/" + _Normal_Icon;
            }
        }
        else if (Marker_Icon == "red") {

            icon.image = "../../Images/Icons/ClientsIcons/" + _Speed_Icon;
        }
    }

    icon.iconSize = new GSize(32, 32);
    icon.dragCrossSize = new GSize(0, 0);
    icon.shadowSize = new GSize(32, 32);
    icon.iconAnchor = new GPoint(5, 5);


    marker = new GMarker(point, icon);
    marker.id = ID;


    GEvent.addListener(marker, 'click',
        function() {
            popupContentHTML = Device_Option_forGoogle(this.id);
            this.openInfoWindowHtml(popupContentHTML);
        }
        );

    GEvent.addListener(marker, 'mouseover',
                         function() {
                             //PreInfo = getDeviceInfoForPopUp(this.id, ClientID, "En");

                             jQuery.ajax({
                                 type: "POST",
                                 url: "../../../DevicesManagerAjax.asmx/getDeviceInfoForPopUp",
                                 data: "{deviceID: '" + this.id + "', IDclient: '" + ClientID + "', Page: 'En'}",
                                 contentType: "application/json; charset=utf-8",
                                 dataType: "json",
                                 timeout: 70000,
                                 success: function (msg) {
                                     var res = msg.d;
                                     test1234(res);
                                 },
                                 error: function (xhr, status, errorThrown) {
                                     alert("An error occered, " + errorThrown);
                                 }
                             });



                         });



    var markers = [];
    markers.push(marker);

    mgr.addMarkers(markers, 0);
    mgr.refresh();
    ClientPOI.refresh();
    POImgr.refresh();
}

It's the same code for calling Javascript function from jQuery. 这与从jQuery调用Javascript函数的代码相同。 It's working fine. 一切正常。 The problem is elsewhere in your code. 问题出在代码的其他地方。

This might not be the exact solution, but given not to divert from the solution. 这可能不是确切的解决方案,但请注意不要偏离解决方案。

function test1234(res) {
    alert(res)
}

function Test() {
    jQuery.ajax({
        type: "POST",
        url: "/TestWebApp/ProcessServlet.do",
        success: function (res) {
            alert(res);
            test1234(res);
        },
        error: function (xhr, status, errorThrown) {
            alert("An error occered, " + errorThrown);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM