Jquery Ajax 点击 href 时调用发送数据到方法
Jquery Ajax Call to send data to the method when the a href clicked
我正在尝试将从我的主页点击的语言类型发送到 App_code
文件夹中的 surfacecontroller
。我的 HTML 如下所示:
<div class="navbar-topbar clearfix">
<div class="h5 pull-right">
<span class="linkCA">
<strong>CA</strong>
<a class="lang=en-CA" href="@NewUrlLink">EN</a> |
<a class="lang=fr-CA" href="@NewUrlLink">FR</a>
</span>
<span class="linkUS">
<strong>US</strong>
<a class="lang=en-US" href="@NewUrlLink">EN</a>
</span>
</div>
</div>
所以我想的是获取 class 名称并在单击 a
时通过 AJAX 调用发送它。我有这个方法,我想将点击的语言类型传递给:
public string GetDictionaryItemByCulture(string key, string language)
{
var currentLang = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
var otherLang = myCulture(language);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(otherLang);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(otherLang);
// String noLastSegment = Request.Url.AbsolutePath;
string x = new UmbracoHelper().GetDictionaryValue(key);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(currentLang);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(currentLang);
return x;
}
我在网上看到了这个 AJAX 方法,它看起来像我需要的,但我不知道如何连接它来满足我的需要。
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: { someParameter: "some value" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
首先,最好将语言环境值放在 HTML 的 data-*
属性中,每个 a
给定一个公共 class 事件处理程序可以附加到:
<div class="navbar-topbar clearfix">
<div class="h5 pull-right">
<span class="linkCA">
<strong>CA</strong>
<a data-lang="en-CA" class="lang-trigger" href="@NewUrlLink">EN</a> |
<a data-lang="fr-CA" class="lang-trigger" href="@NewUrlLink">FR</a>
</span>
<span class="linkUS">
<strong>US</strong>
<a data-lang="en-US" class="lang-trigger" href="@NewUrlLink">EN</a>
</span>
</div>
</div>
从那里您可以点击 a
,读取 data-lang
属性并发送信息:
$('.lang-trigger').click(function() {
var $a = $(this);
$.ajax({
type: "POST",
url: "GetDictionaryItemByCulture/",
data: {
key: 'whatever this value should be...',
lang: $a.data('lang')
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
console.log(msg);
}
});
});
我正在尝试将从我的主页点击的语言类型发送到 App_code
文件夹中的 surfacecontroller
。我的 HTML 如下所示:
<div class="navbar-topbar clearfix">
<div class="h5 pull-right">
<span class="linkCA">
<strong>CA</strong>
<a class="lang=en-CA" href="@NewUrlLink">EN</a> |
<a class="lang=fr-CA" href="@NewUrlLink">FR</a>
</span>
<span class="linkUS">
<strong>US</strong>
<a class="lang=en-US" href="@NewUrlLink">EN</a>
</span>
</div>
</div>
所以我想的是获取 class 名称并在单击 a
时通过 AJAX 调用发送它。我有这个方法,我想将点击的语言类型传递给:
public string GetDictionaryItemByCulture(string key, string language)
{
var currentLang = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
var otherLang = myCulture(language);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(otherLang);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(otherLang);
// String noLastSegment = Request.Url.AbsolutePath;
string x = new UmbracoHelper().GetDictionaryValue(key);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(currentLang);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(currentLang);
return x;
}
我在网上看到了这个 AJAX 方法,它看起来像我需要的,但我不知道如何连接它来满足我的需要。
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: { someParameter: "some value" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
首先,最好将语言环境值放在 HTML 的 data-*
属性中,每个 a
给定一个公共 class 事件处理程序可以附加到:
<div class="navbar-topbar clearfix">
<div class="h5 pull-right">
<span class="linkCA">
<strong>CA</strong>
<a data-lang="en-CA" class="lang-trigger" href="@NewUrlLink">EN</a> |
<a data-lang="fr-CA" class="lang-trigger" href="@NewUrlLink">FR</a>
</span>
<span class="linkUS">
<strong>US</strong>
<a data-lang="en-US" class="lang-trigger" href="@NewUrlLink">EN</a>
</span>
</div>
</div>
从那里您可以点击 a
,读取 data-lang
属性并发送信息:
$('.lang-trigger').click(function() {
var $a = $(this);
$.ajax({
type: "POST",
url: "GetDictionaryItemByCulture/",
data: {
key: 'whatever this value should be...',
lang: $a.data('lang')
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
console.log(msg);
}
});
});