How to get jQuery to accept json response?

Here are some simple Javascript:

(function($){
    var ajax_callback = function(data) { window.location.hash = data.h1; };
    $('.clickable').live('click', function() { 
             $.post('server.fcgi', {}, ajax_callback, 'json');
         }
    );
})(jQuery);

The server is a binary C ++ (yes, I know), which throws (via fast_cgi) line: {"h1":"newhash"}.

The expected behavior is that the URL should change. Instead, nothing happens and Firebug complains that the β€œdata” is β€œnull” !. Any help would be greatly appreciated!

Will.

When the following code enters "ajax_callback", it says that "data" is "null" !. But the server is a C ++ binary, which is confirmed by returning a JSON string {"h1":"newhash"}. Does anyone have an idea why jQuery seems unable to accept JSON data when calling ajax_callback?

+3
3

, HTTP . :.

HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: ...
...

{"h1":"bla"}

, printf("{\"h1\":\"bla\"}"); .

, , HEAD, GET, wget, curl nc. , Net Firebug ..

0

, $.POST(). , jquery $.post. ( "JSON" ), . , , , .

$.post('server.fcgi', {}, ajax_callback,{}, 'json');

-, JSON, $.parseJSON() .

, , URL- JSON, JSON .

.

$.post("url/path/here/to/json", {}, function(data){

    if(data){ // just in case the called program had a problem
         var obj =  $.parseJSON(data);
        .... do everything else using the Obj->         
    }
},{},"json");

.

JQuery, JSON,

$.getJSON();

url

.

$(document).ready(function(){

    $('.clickable').live('click', function() { 
             $.getJSON('server.fcgi', function(data){
                         window.location.hash = data.h1;
              });
         }
    );
});
+2

, , , , jQuery 1.4.2? , 1.3.2, .: (

0

Source: https://habr.com/ru/post/1752034/


All Articles