2013-10-28 35 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