この質問のタイトルが正しいかどうかわからないので、誰かがより良い説明を持っている場合は編集してください。
次のようにカスタム属性を持つ要素をアンカーしました(更新:@Runeソリューションと@Shaunaコメントによる)
<a href="#" class="call-method" data-url="/Pages/mustafa/test_dialoge.aspx/GetDate" data-data="" data-success="handleResult">call me</a>
data-url、data-data、data-success、data-error はカスタム属性です。
次に、次のスニペットを使用して、いくつかのスクリプトを添付します
$('.call-method').live('click', function () {
var obj = $(this);
var options = new Object();
options.url = obj.attr('data-url').toString();
options.success = obj.attr('data-success');
options.error = obj.attr('data-error');
options.complete = obj.attr('data-complete');
var _options = JSON.stringify(options);
callServerGetMethod(_options);
});
var callServerGetMethod = function (options) {
var _options = new Object();
var self = this;
if (typeof options === 'string') {
_options = JSON.parse(options, function (key, value) {
if (typeof self[value] === 'function') {
return eval(self[value]);
}
return value;
});
}
$.ajax(
{
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
async: true,
cache: false,
error:
function (xhr, ajaxOptions, thrownError) {
console.log(xhr.statusText);
console.log(thrownError);
}
}, $.extend(_options));
};
今、ブラウザコンソールに次のエラーが表示されました
SyntaxError {popStackFrame: function}
arguments: Array[1]
get message: function () { [native code] }
get stack: function () { [native code] }
set message: function () { [native code] }
set stack: function () { [native code] }
type: "unexpected_token"
__proto__: Error
arguments: undefined
constructor: function SyntaxError() { [native code] }
name: "SyntaxError"
stack: undefined
type: undefined
__proto__: d