有人能找出这段代码中的错误吗? 我试图调用我的API控制器返回JsonObject。它击中控制器,结果显示在Chrome>中预习。但是,当我尝试访问成功函数时,它显示未定义。
$(document).ready(function () {
CallService();
});
function CallService() {
$.ajax({
type: "GET",
url: "http://localhost:8080/Java/Controller/Method",
contentType: "application/json; charset=utf-8",
dataType: "script",
success: function (data) {
alert('success '+ data);
},
error: function (data) {
alert('error ' + data);
}
});
}
有趣的是,如果我输入dataType:"脚本"我只能点击控制器。在json或jsonp的情况下。我无法击中控制器上的事件。
修改
使用dataType控制台登录:" jsonp"
object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
abort: function (a){var b=a||u;return i&&i.abort(b),x(0,b),this}
always: function (){return e.done(arguments).fail(arguments),this}
complete: function (){if(h){var d=h.length;!function f(b){n.each(b,function(b,c){var d=n.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}
done: function (){if(h){var d=h.length;!function f(b){n.each(b,function(b,c){var d=n.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}
error: function (){if(h){var d=h.length;!function f(b){n.each(b,function(b,c){var d=n.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}
fail: function (){if(h){var d=h.length;!function f(b){n.each(b,function(b,c){var d=n.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}
getAllResponseHeaders: function (){return 2===t?f:null}
getResponseHeader: function (a){var b;if(2===t){if(!j){j={};while(b=Dc.exec(f))j[b[1].toLowerCase()]=b[2]}b=j[a.toLowerCase()]}return null==b?null:b}
overrideMimeType: function (a){return t||(k.mimeType=a),this}
pipe: function (){var a=arguments;return n.Deferred(function(c){n.each(b,function(b,f){var g=n.isFunction(a[b])&&a[b];e[f[1]](function(){var a=g&&g.apply(this,arguments);a&&n.isFunction(a.promise)?a.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[f[0]+"With"](this===d?c.promise():this,g?[a]:arguments)})}),a=null}).promise()}
progress: function (){if(h){var d=h.length;!function f(b){n.each(b,function(b,c){var d=n.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}
promise: function (a){return null!=a?n.extend(a,d):d}
readyState: 4
setRequestHeader: function (a,b){var c=a.toLowerCase();return t||(a=s[c]=s[c]||a,r[a]=b),this}
state: function (){return c}
status: 200
statusCode: function (a){var b;if(a)if(2>t)for(b in a)q[b]=[q[b],a[b]];else v.always(a[v.status]);return this}
statusText: "success"
success: function (){if(h){var d=h.length;!function f(b){n.each(b,function(b,c){var d=n.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}
then: function (){var a=arguments;return n.Deferred(function(c){n.each(b,function(b,f){var g=n.isFunction(a[b])&&a[b];e[f[1]](function(){var a=g&&g.apply(this,arguments);a&&n.isFunction(a.promise)?a.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[f[0]+"With"](this===d?c.promise():this,g?[a]:arguments)})}),a=null}).promise()}
__proto__: Object
答案 0 :(得分:2)
function CallService() {
$.ajax({
type: "GET",
url: "http://localhost:8080/Java/Controller/Method",
contentType: "application/json; charset=utf-8",
success: function (data) {
data = jQuery.parseJSON(data.d)
alert('success '+ data[0].yourvalues);
},
error: function (data) {
alert('error ' + data);
}
});
}
答案 1 :(得分:2)
试试这个:
console.log(data);
它在您的控制台中显示jsonObject
。
如果您想提醒数据,请尝试以下操作:
alert(data.jsonObectName[index].attributeName);
此处索引是对象索引,即以0开头,属性名称是对象
中的值名称$.ajax({
type: "GET",
url: "http://localhost:8080/Java/Controller/Method",
contentType: "application/json; charset=utf-8",
dataType: "script",
success: function (data) {
console.log(data);
alert('success '+ data.jsonObectName[index].attributeName);
},
error: function (data) {
console.log(data);
alert('error ' + data.jsonObectName[index].attributeName);
}
});
答案 2 :(得分:2)
您的AJAX成功方法中有对象。然后你可以从响应中绑定数据。
在这里供你参考我从服务器附加了简单的JSONP响应并绑定了UserInterface中的值。
$.ajax({
type: 'GET',
url: 'http://githubbadge.appspot.com/Jebasuthan',
dataType: 'jsonp',
success: function (json) {
console.log(json);
var result = '<h3>Name: ' + json.user.login + '</h3>' +
'<p>Forked Repository: ' + json.fork_repos + '</p>' +
'<p>Languages: ' + json.languages + '</p>' +
'<p>Email-Id: ' + json.user.email + '</p>' +
'<p>Location: ' + json.user.location + '</p>' +
'<p>Last Updated Date: ' + json.user.updated_at + '</p>' +
'<p>Blog: <a target="_blank" href=' + json.user.blog + '>' + json.user.blog + '</a></p>';
$('#badge').append(result);
}
});