admin管理员组

文章数量:1391960

I'm using the following code to get the dataa from the server:

  $.getJSON('.svc/SampleMethod?callback=?', dd, function (data) {
                alert(data);
  });

From the server, I'm sending the byte array as response. In firebug, in Net > Response tab, I get:

jQuery19101878696953793153_1365677709012([67,37,94,38,42,44,69,67,71,32,97,116,116,97,99,104,101,100,32,102,111,114,32,112,97,116]);

Also in Net > JSON tab, I get data with several keys.

But how to get the data at alert(data);; so that I process on that data. I don't know, how this thing works.

Edit:

I tried this different approach:

 $.ajax({
                type: "GET",
                dataType: "jsonp",
                contentType: "application/javascript",
                data: dd,
                crossDomain: true,
                url: ".svc/SampleMethod",
                success: function (data) {
                    alert(JSON.parse(data));
                },
                plete: function (request, textStatus) { //for additional info
                    alert(request.responseText);
                    alert(textStatus);
                },
                error: function(request, textStatus, errorThrown) {
                    alert(textStatus);
                  }
            });

But I got: parseerror as alert.

I'm using the following code to get the dataa from the server:

  $.getJSON('http://xxx.xxx.xxx.xx/SampleWebService/Service.svc/SampleMethod?callback=?', dd, function (data) {
                alert(data);
  });

From the server, I'm sending the byte array as response. In firebug, in Net > Response tab, I get:

jQuery19101878696953793153_1365677709012([67,37,94,38,42,44,69,67,71,32,97,116,116,97,99,104,101,100,32,102,111,114,32,112,97,116]);

Also in Net > JSON tab, I get data with several keys.

But how to get the data at alert(data);; so that I process on that data. I don't know, how this thing works.

Edit:

I tried this different approach:

 $.ajax({
                type: "GET",
                dataType: "jsonp",
                contentType: "application/javascript",
                data: dd,
                crossDomain: true,
                url: "http://xxx.xxx.xxx.xx/SampleWebService/Service.svc/SampleMethod",
                success: function (data) {
                    alert(JSON.parse(data));
                },
                plete: function (request, textStatus) { //for additional info
                    alert(request.responseText);
                    alert(textStatus);
                },
                error: function(request, textStatus, errorThrown) {
                    alert(textStatus);
                  }
            });

But I got: parseerror as alert.

Share Improve this question edited Apr 11, 2013 at 12:15 mike44 asked Apr 11, 2013 at 10:56 mike44mike44 8125 gold badges15 silver badges38 bronze badges 6
  • data is that array, isn't it (what do you get from the alert)? Just use it. – Bergi Commented Apr 11, 2013 at 11:11
  • @Bergi, the problem is I don't get any alert. – mike44 Commented Apr 11, 2013 at 11:16
  • Hm. Do you get an error? Append .fail(alert.bind(window));, or console.log the object that $.getJSON returns. – Bergi Commented Apr 11, 2013 at 11:53
  • @Bergi, No. Please see the update. – mike44 Commented Apr 11, 2013 at 12:15
  • 1 Check your network tab whether the large response is plete. I've heard of cases where the server (or something in the munication channel) chops of very long responses. And if it's plete, maybe it really is invalid; let your debugger halt on scripting errors or post the response into a linter. Btw: You don't need JSON.parse with JSONP, as your callback gets passed an object literal not a JSON-string-literal! – Bergi Commented Apr 11, 2013 at 13:32
 |  Show 1 more ment

4 Answers 4

Reset to default 2

From looking at the docs (I haven't tried this) you need to explicitly tell jQuery that you're making a JSONP call that will invoke the function that's returned. Something like this:-

 $.ajax({
     type : "GET",
     dataType : "jsonp",
     url : "http://xxx.xxx.xxx.xx/SampleWebService/Service.svc/SampleMethod",
     success: function(data){
           alert(data);
     }
});

Function you are looking for is JSON.parse. Please try this code :

$.post("YouURL", { 'ParameterName': paramvalue }, function (Data) {
  var data = JSON.parse(data);
});

Your response is a function call. If u define function name with name jQuery19101878696953793153_1365677709012 you can process the 'data' else from your server just send the json as a response to $.getJSON's callback to work

The problem was the data was very huge. I was sending array of around 10,00,000+ bytes. So instead I divided it into list of bytes (each having 1000 bytes) & then sent as response.

I don't know if this is the best solution, but it solved my problem. BTW, thanks to all for helping me.

本文标签: javascriptGetting data through jQuery ajax requestStack Overflow