我不知道这个问题的标题是否正确,所以如果有人有更好的描述,请编辑它。
我有如下自定义属性的锚元素(更新:根据@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