的getJSON回调没有发射 [英] getJSON callback not firing
问题描述
我用下面的脚本被称为上点击一个锚标记进行调用
函数为gettoken(VIDEOID){
调试器;
VAR JSON = $ .getJSON(/ VOD / RequestAccessToken /+ VIDEOID,功能(结果){
警报(令牌:收到:+ result.token);
});
}
在服务器应用程序,我接听电话,所以我知道这是一个有效的URL,但回调没有被调用。如果我设置虽然jQuery的code(F11 / F10)的回调被称为?? !!!?
从MVC应用程序服务器返回结果中的一类形式
//函数调用
公共JsonResult RequestAccessToken(INT ID)
{
令牌T =新的令牌();
t.MasterId = Guid.NewGuid();
VAR的结果=新TokenResult(t.MasterId);
返回this.Json(结果,JsonRequestBehavior.AllowGet);
}
//类返回
公共类TokenResult
{
公共TokenResult(){}
公共TokenResult(GUID G){记号= g.ToString(); }
公共字符串标记= NULL;
}
当我通过浏览器访问结果的URL =
{
令牌:c877453e-739d-4883-9310-91ddd707d6af
}
如果你的结果不成功,该回调将不火,这通常是由于返回无效JSON。为了给它一个测试,你可以使用 $的长表格的getJSON
,像这样的,所以你可以看到错误:
$。阿贾克斯({
网址:网址,
数据类型:JSON,
成功:函数(结果){
警报(令牌:收到:+ result.token);
},
错误:函数(请求,textStatus,errorThrown){
警报(textStatus);
},
完成:功能(请求,textStatus){//,了解更多信息
警报(request.responseText);
警报(textStatus);
}
});
如果它是一个JSON /解析错误,你可以把你的反应,看看有什么不对JSONLint,在这里: HTTP://www.jsonlint .COM /
I'm making the call using the following script which is called on click of an anchor tag
function GetToken(videoId) {
debugger;
var json = $.getJSON("/Vod/RequestAccessToken/"+videoId, function(result){
alert("token recieved: " + result.token);
});
}
In the server application I receive the call so I know it is a valid URL, but the callback is not being invoked. If i set though the jquery code (f11/f10) the callback is called??!!!?
Server returns results from MVC application in the form of a class
// function called
public JsonResult RequestAccessToken(int id)
{
Token t = new Token();
t.MasterId = Guid.NewGuid();
var result = new TokenResult(t.MasterId);
return this.Json(result, JsonRequestBehavior.AllowGet);
}
// class returned
public class TokenResult
{
public TokenResult() { }
public TokenResult(Guid g) { token = g.ToString(); }
public string token = null;
}
When I access the url via browser result =
{
"token":"c877453e-739d-4883-9310-91ddd707d6af"
}
If your result is not successful, that callback won't fire, this is usually due to invalid JSON being returned. To give it a test, you can use the long form of $.getJSON
, like this so you can see the error:
$.ajax({
url: url,
dataType: 'json',
success: function(result){
alert("token recieved: " + result.token);
},
error: function(request, textStatus, errorThrown) {
alert(textStatus);
},
complete: function(request, textStatus) { //for additional info
alert(request.responseText);
alert(textStatus);
}
});
If it is a JSON/parser error, you can take your response and see what's wrong with JSONLint, here: http://www.jsonlint.com/
这篇关于的getJSON回调没有发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!