簡體   English   中英

將數據從javascript ajax函數發送到jsp

[英]Send data from a javascript ajax function to a jsp

這就是我想要做的。 在主頁上,例如/home.jsp,用戶單擊鏈接。 我讀取了鏈接的值,並在此基礎上調用了RESTful資源,該資源又操縱數據庫並返回響應。 與REST的交互按預期發生在使用JavaScript的情況下。 我已經能夠從REST資源中獲取信息,但是現在我想將該數據發送到另一個JSP ..說/info.jsp。 我無法做到這一點。

我試圖在父Ajax調用的成功函數中進行另一個Ajax調用,但是沒有任何反應。 例如:

function dealInfo(aparameter){



    var requestData = {
            "dataType":    "json",
            "type"    :    "GET",
            "url"     :    REST resource URL+aparameter,
    };

    var request = $.ajax(requestData);
    request.success(function(data){

        alert(something from data); //this is a success

        //I cannot get into the below AJAX call
        $.ajax({

            "type": "post",
            "url": "info.jsp"
            success: function(data){
                alert("here");
                ("#someDiv").html(data[0].deviceModel);
            }

        });

我該如何實現這一目標? 我應該使用其他方法而不是兩次Ajax調用嗎? 任何幫助表示贊賞。 謝謝。

您可以使用以下功能:

function dealInfo(aparameter) {
    $.ajax({
        url: 'thePage.jsp',
        type: "GET",
        cache: false,
        dataType: 'json',
        data: {'aparameter': aparameter},
        success: function (data) {

            alert(data); //or you can use console.log(data);

            $.ajax({
                url: 'info.jsp',
                type: "POST",
                cache: false,
                data: {'oldValorFromFirstAjaxCall': data},
                success: function (info) {
                    alert(info); //or you can use console.log(info);
                    $("#someDiv").html(info);
                }
            });

        }
    });
}

或使AJAX調用同步:

function dealInfo(aparameter) {
    var request = $.ajax({
        async: false, //It's very important
        cache: false,
        url: 'thePage.jsp',
        type: "GET",
        dataType: 'json',
        data: {'aparameter': aparameter}
    }).responseText;

    $.ajax({
        url: 'info.jsp',
        type: "POST",
        cache: false,
        data: {'oldValorFromFirstAjaxCall': request},
        success: function (info) {
            alert(info); //or you can use console.log(info);
            $("#someDiv").html(info);
        }
    });
}

我正在用這種方式。

“ type”:“ post”而不是type:'post'


也許會有所幫助。 請嘗試一下。 例如;

$.ajax({
      url: "yourURL",
      type: 'GET',
      data: form_data,
      success: function (data) {
      ...
      }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM