开发者

strange behaviour with json object coming from ajax call in safari/webkit

I'm using jquery to make an AJAX POST call to a web service, and getting a JSON object back, which gives me back some html code that i want to append to a div, it works fine in firefox but the problem is that safari doesn't do the appending, here is the example:

$.ajax({
    type: "POST",
    url: "ConnMgr.asmx/Request",
    data: JSON.stringify(objectToSend),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(respons开发者_StackOverflow社区e){
        $('#myDiv').empty();
        $("#myDiv").append(response.d.htmlSnippet); //this doesn't work on safari but it does on FF
        //$("#myDiv").append("<img src=\"image.png"/>")//this works in all browsers
        //alert(response.d);//this works in all browsers
    }
});

It seems that in safari, jquery doesn't like the idea of using a json object as an argument for append() I've tried creating a copy of the variable before, inserting a delay, converting the variable to string before passing it, but the results are the same.

Many thanks


did you try response.d.htmlSnippet.ToString()


You mean something like this http://jsbin.com/elapa/ doesn't work for you in safari?


yes i did try using response.d.htmlSnippet.ToString() and it didn't help

finally i did a workaround by composing the htmlsnippet and then taking only one number from the coming JSON object, and this way it worked safari debugging console didn't report any error


Not to be nitpicky, but isn't this block the same as

success: function(response) {
    $('#myDiv').empty();


    //this doesn't work on safari but it does on FF
    //$("#myDiv").append("<img src=\"image.png"/>")//this works in all browsers
    //alert(response.d);//this works in all browsers
    $("#myDiv").append(response.d.htmlSnippet);
}

as this block because you can chain method calls in jQuery?

success: function(response) {
    $('#myDiv').html(response.d.htmlSnippet);
}



  1. Can you try doing something like this?

    $('#myDiv').html( '' + response.d.htmlSnippet );
    

    I don't know if it will work or not...but it's worth a try.

  2. I think your code response.d.htmlSnippet.ToString() might not work.
    It should be a lowercase "toString()".


There is a difference in JSON implementation between FF and others that I spotted once - others don't allow weird charactersto be passed. You would have to use entities. Try seeing for sure what comes back - drop the whole response object to a firebug-alike console and see the content. alerting it might not be enough.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜