English 中文(简体)
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(response){
        $( #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.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签