从服务器接收数据时出现Ajax jquery问题

时间:2015-11-26 14:09:30

标签: jquery ajax

我正在尝试从Web服务器获取回复。为此,我使用ajax脚本发送请求并获取json或xml数据。 Web服务器是Indy。我不知道这种服务器,也没有任何修改配置的可能性。

我已经完成了两个js脚本。 第一个发送请求到Web服务器Django。在修改配置以允许CROS请求后,客户端会正确地从服务器获取数据。 第二个向Indy Web服务器发送请求。在修改了ajax请求(datatype =“script”)之后,我得到了一个回复,但是这些是空的。

我发现的两个回复之间的差异来自回复的标题。 从Indy Web服务器,我得到:

  

Content-Disposition:“inline; filename =”toto.dat“”
  内容类型:“应用/八位字节流”

相反,来自Django的是

  

的Content-Type: “应用/ JSON”

这是我的代码

    $("#button1").click(function(){
    alert("Script 1");

    // Script to request on Django web server

     $.ajax({
        url: "http://localhost:80/polls/getid",
        crossDomain: true,
        dataType: "json",
        success: function (xhr, status, response) {
            alert("coucou");
            var resp = JSON.parse(response.responseText);
            alert(resp.id);
        },
        error: function (xhr, status) {
            alert("error");
        }
    });
});


$("#button2").click(function(){
    alert("Script 2");

    // Script to Indy web server

     $.ajax({
        url: "http://localhost:8080/index_xml?callback=?",
        crossDomain: true,
        dataType: "script",
        success: function (xhr, status, response) {
            alert(response);
            var resp = JSON.parse(response.responseText);
            alert(resp.id);
        },
        error: function (xhr, status) {
            alert("error");
        }
    });
});

这些是标题:

标题回复脚本1

  

HTTP / 1.0 200 OK
  日期:2015年11月26日星期四13:40:00 GMT
  服务器:WSGIServer / 0.1 Python / 2.7.9
  access-control-allow-origin:*
  内容类型:application / json
  X-Frame-Options:SAMEORIGIN

标题回复脚本2

  

HTTP / 1.1 200 200 OK
  连接:关闭
  内容类型:application / octet-stream
  内容长度:92
  内容处理:内联;文件名= “job00ZT6VI8NQO9O09479E0E1B.dat”
  服务器:Indy / 9.0.18

您对如何处理Indy回复的js脚本有所了解吗?我想我收到的是一个文件而不是一些数据。但我很失落。

3 个答案:

答案 0 :(得分:0)

    $("#button1").click(function(){
    alert("Script 1");

    // Script to request on Django web server

     $.ajax({
        url: "http://localhost:80/polls/getid",
        crossDomain: true,
        dataType: "json",
        success: function (xhr, status, response) {
            alert("coucou");
            var resp = JSON.parse(response.responseText);
            alert(resp.id);
        },
        error: function (xhr, status) {
            alert("error");
        }
    });
});


$("#button2").click(function(){
    alert("Script 2");

    // Script to Indy web server

     $.ajax({
        url: "http://localhost:8080/index_xml?callback=?",
        crossDomain: true,
        dataType: "script",
        success: function (xhr, status, response) {
            alert(response);
            var resp = JSON.parse(response.responseText);
            alert(resp.id);
        },
        error: function (xhr, status) {
            alert("error");
        }
    });
});

答案 1 :(得分:0)

当您尝试访问dataType: "script"中的dataType: "text"时,为什么要设置response.responseText而不是success()?没有任何意义,所以将dataType更改为text,它应该有效。

您也可以尝试使用converters设置。

答案 2 :(得分:0)

我找到了解决方案。 我第一次读到JSONP,我还没有真正理解。所以我尝试了几件事没有成功。

我再次阅读有关JSONP的内容,最后了解到当ajax发送带有回调值的查询时,服务器必须使用此属性作为回复客户端的函数。

示例:

  

AJAX方面:

http://...?callback=jQuery111109043251063544318_1448672067019
  

服务器端回复:

jQuery111109043251063544318_1448672067019({"id":"1234"});

@ViRuSTriNiTy非常感谢您的帮助

相关问题