我正在发送一个url来获取json响应,但是我得到了响应而不是“正确的响应”(json数据)..这里有什么问题..
var JsonHandler = function(){
return{
json : function(url){
return $.getJSON(url, function(data){
return data;
});
}
}
};
(function ($) {
var path = "js/data.json";
var getJson = new JsonHandler();
console.log(getJson.json(path));// i am getting a object instead of json response..
})(jQuery);
答案 0 :(得分:1)
$.getJSON
不返回JSON。它是异步的。在回调中调用return
不会改变这一点。
相反,请使用:
var JsonHandler = function(callback){
return {
json : function(url){
return $.getJSON(url, callback);
}
}
};
(function ($) {
var path = "js/data.json";
var getJson = new JsonHandler(function(data){console.log(data)});
getJson.json(path);
})(jQuery);
答案 1 :(得分:1)
您需要在回调中处理逻辑。你应该怎么做取决于你的代码。
如果您使用console.dir(data)
代替return data;
,则会看到您的回复。
所以你应该把相应的代码放在那里应该对数据做些什么。或者我们jQuery.proxy
或Function.prototype.bind
将您的回调绑定到Object。但无论您如何创建传递给getJson
的回调,它都必须是您使用数据的执行路径的 origin 。
为了说明异步行为,请检查代码的这种修改:
var JsonHandler = function(){
return{
json : function(url){
return $.getJSON(url, function(data){
console.log("data received");
console.dir(data);
});
}
}
};
(function ($) {
var path = "js/data.json";
var getJson = new JsonHandler();
console.log("before json request");
getJson.json(path);// i am getting a object instead of json response..
console.log("after json request");
})(jQuery);
修改强>
这里有一个pub / sub系统的示例(代码当前是更多的伪代码 - 没有测试它的语法错误,但我会在几个小时内修复可能存在的错误)
var JsonHandler = {
request : function( url, params, key ) {
$.getJSON(url, params, function(data){
//because of js scopes the 'key' is the one that is used for requesting the data
JsonHandler._publishData(data, key);
});
},
_subcribers : {},
/*
key could also be path like this:
/news/topic1
/news/topic2
/news
so code subscribes for '/new' it will be notifed for /news/topic1, /news/topic2, /news
*/
subscribe : function( key, callback ) {
/*
The path logic is missing here but should not be that problematic to added if you understood the idea
*/
this._subcribers[key] = this._subcribers[key]||[];
this._subcribers[key].push(callback);
},
_publishData : function( data, key ) {
/*
check if there are subscribers for that key and if yes call notify them
This part is also missing the path logic mention above, is just checks for the key
*/
if ( this._subcribers[key] ) {
var subscribers = this._subcribers[key];
for( var i = 0, count = subscribers.length ; i<count ; i++ ) {
subscribers[i](data,key);
}
}
}
}
var AnObject = function() {
}
AnObject.prototype = {
updateNews : function(data, key) {
console.log("received data for key: "+ key);
console.dir(data);
}
};
var obj = new AnObject();
//add a simple callback
JsonHandler.subscribe( "/news", function(data, key ) ) {
console.log("received data for key: "+ key);
console.dir(data);
});
//add a callback to a 'method' of an object
JsonHandler.subscribe( "/news", obj.updateNews.bind(obj) );
JsonHandler.request("some/url",{/* the parameters */}, "/news");
答案 2 :(得分:0)
$.ajax({
type: "POST",
url: "frmTrialBalance.aspx/GetTheTrialBalanceOriginalCurrPriorTwoQuarter",
data: "{ CompanyId:'" + s + "',EndDate:'" + datet + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
***var data = $.parseJSON(msg.d);***
LoopGetTrialBalanceORIGINALPriorYearMonthQuarter(data);
IncreaseIframeHeight();
},
error: AjaxFailed
});*emphasized text*
答案 3 :(得分:0)
你必须在回调函数中执行所有代码,在那里你得到数据对象。 $ .getJSON返回XMLHTTPRequest对象。
var JsonHandler = function () {
return {
json: function (url) {
$.getJSON(url, {}, function (data) {
console.log(data);
});
}
}
};
(function ($) {
var path = "js/data.json";
var getJson = new JsonHandler();
getJson.json(path); // i am getting a object instead of json response..
})(jQuery);