2013-10-28 76 views
0

我將「100 MYR」以下的值轉換爲不同的國家/地區貨幣。轉換器使用JQuery(Google API)。我想在下面的另一個頁面中將值(轉換幣種)傳遞給標籤(lblAmountPaid)。我嘗試使用會話和cookie方法,但無法工作,它返回空字符串。請幫忙,謝謝。從JQuery向ASP.NET提取值

enter image description here

ccGOOG.js

$(document).ready(function() { 
$('#submit').click(function() { 
    var errormsg = ""; 
    var amount = $('#txtAmount').val(); 
    var from = $('#drpFrom').val(); 
    var to = $('#drpTo').val(); 
    $.ajax({ type: "POST", 
     url: "WebService.asmx/ConvertGOOG", 
     data: "{amount:" + amount + ",fromCurrency:'" + from + "',toCurrency:'" + to + "'}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     beforeSend: function() { 
      $('#results').html("Converting..."); 
     }, 
     success: function (data) { 
      $('#results').html(amount + ' ' + from + '=' + data.d.toFixed(2) + ' ' + to); 
     }, 

     error: function (jqXHR, exception) { 
      if (jqXHR.status === 0) { 
       errormsg = 'Not connect.\n Verify Network.'; ; 
      } else if (jqXHR.status == 404) { 
       errormsg = 'Requested page not found. [404]'; ; 
      } else if (jqXHR.status == 500) { 
       errormsg = 'Internal Server Error [500].'; ; 
      } else if (exception === 'parsererror') { 
       errormsg = 'Requested JSON parse failed.'; ; 
      } else if (exception === 'timeout') { 
       errormsg = 'Time out error.'; ; 
      } else if (exception === 'abort') { 
       errormsg = 'Ajax request aborted.'; ; 
      } else { 
       errormsg = 'Uncaught Error.'; 
      } 
      $('#results').html(errormsg); 
      $('<a href="#" >Click here for more details</a>').click(function() { 
       alert(jqXHR.responseText); 
      }).appendTo('#results'); 
     } 
    }); 
}); 
}); 

下面是另一個頁面:

enter image description here

回答

0

我建議你調用一個ASP.NET AJAX頁面方法你success回調.ajax()方法,如thi S:

首先,這裏是第方法與Session啓用:

[WebMethod(EnableSession = true)] 
public static void SetAmountInSession(int amount) 
{ 
    HttpContext.Current.Session["amount"] = amount; 
} 

接下來,你需要從success回調了jQuery .ajax()方法的調用此方法,傳遞給它的谷歌API調用的結果,像這樣:

success: function (data) { 
    $('#results').html(amount + ' ' + from + '=' + data.d.toFixed(2) + ' ' + to); 
    var args = { 
     amount: data.d.toFixed(2) 
    }; 
    $.ajax({ 
     type: "POST", 
     url: "YourPage.aspx/SetSession", 
     data: JSON.stringify(args), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function() { 
      alert('Success.'); 
     }, 
     error: function() { 
      alert("Fail"); 
     } 
    }); 
}, 

最後,在 「其他」 頁面中,您可以在Session搶值,就像這樣:

// Check if value exists before we try to use it 
if(Session["amount"] != null) 
{ 
    lblTotalAmount.Text = Session["amount"].ToString(); 
} 
+0

謝謝你的回覆。我關注了一切,當我點擊轉換按鈕時,它顯示一個消息框顯示「失敗」,並且沒有任何值返回到其他頁面。我不明白爲什麼。 – Roshan