javascript 通过 jQuery ajax 请求获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15946956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:42:17  来源:igfitidea点击:

Getting data through jQuery ajax request

javascriptjqueryajaxjsonjsonp

提问by mike44

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 > Responsetab, I get:

从服务器,我发送字节数组作为响应。在firebug,在Net > Response选项卡中,我得到:

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 > JSONtab, I get data with several keys.

同样在Net > JSON选项卡中,我使用多个键获取数据。

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

但是如何在alert(data);; 以便我处理该数据。我不知道,这东西是怎么运作的。

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));
                },
                complete: function (request, textStatus) { //for additional info
                    alert(request.responseText);
                    alert(textStatus);
                },
                error: function(request, textStatus, errorThrown) {
                    alert(textStatus);
                  }
            });

But I got: parseerroras alert.

但我得到了:parseerror作为警觉。

回答by tinyd

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:-

通过查看文档(我还没有尝试过),您需要明确告诉 jQuery 您正在进行 JSONP 调用,该调用将调用返回的函数。像这样的东西:-

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

回答by mike44

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.

问题是数据非常庞大。我正在发送大约 10,00,000+ 字节的数组。因此,我将其分成字节列表(每个有 1000 个字节),然后作为响应发送。

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

我不知道这是否是最好的解决方案,但它解决了我的问题。BTW,感谢所有帮助我的人。

回答by ravisolanki07

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

您正在寻找的函数是 JSON.parse。请试试这个代码:

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

回答by Gowsikan

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

您的响应是函数调用。如果您使用名称 jQuery19101878696953793153_1365677709012 定义函数名称,则可以处理来自服务器的其他“数据”,只需发送 json 作为对 $.getJSON 回调的响应即可工作