简体   繁体   中英

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.

here is my code (see the ajax call in function addGMarker):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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