开发者

getJSON callback not firing

开发者 https://www.devze.com 2022-12-28 00:58 出处:网络
I\'m making the call using the following script which is called on click of an anchor tag function GetToken(videoId) {

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/RequestA开发者_JAVA百科ccessToken/" + 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/


A likely bet is that you're not returning valid JSON. The jQuery docs note that, "As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently." Does the JSON text you're serving pass JSON Lint? You may also want to switch (at least temporarily) to jQuery.ajax. It allows more error handling. Something like:

$.ajax({
  url: "/Vod/RequestAccessToken/"+videoId,
  dataType: 'json',
  error: function(xhr, textStatus, errorThrown){

        },
  success: function(result){
            alert("token recieved: " + result.token);
        }
});


From jQuery's documentation:

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently.

So, is your JSON valid?

function GetToken(videoId) {
    debugger;
    console.log('getting it');
    var json = $.get("/Vod/RequestAccessToken/"+videoId, function(result){
        console.log('got it');
    });
}

What does the above output? (Assuming your browser has a console.)


Are you calling preventDefault() or returning false in the anchor's click handler?


You need to signal to allow JSONGets in your controller action when calling the Serialization Json method:

 public JsonResult ActionDateClicked(ActionViewModel vm)
        {
            vm.Model.Observation = "Changed";
            return Json(vm, JsonRequestBehavior.AllowGet);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消