weixin_33724570 2018-01-17 15:04 采纳率: 0%
浏览 31

Ajax无法正常工作

I'm having a problem with my ajax displaying the response in an alert.

I use jquery and ajax. The javascript contains a normal GET-Request. The ajax doesn't go to the .done function. The website displays the second alert:

alert("Sorry. Server unavailable. ");

But in the browser it displays in the console. I'm using firefox + a Plug-in for Firefox (https://addons.mozilla.org/de/firefox/addon/cors-everywhere/reviews/). I don't think the plugin is the proplem because without the plugin it's also working.

So here is my html:

<html>
    <head>
        <meta charset="utf-8" />
        <title>Peyer E1 9.2 Auswahl</title>
        <script src="clearcache.js"></script>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    </head>
    <body>
        <button onclick="data()">Test</button>
    </body>
</html>

Here is my Javascript:

function data() {
    $.ajax({
        type: "GET",
        url: "url",
        dataType: "application/json",
        headers: {
            "Authorization": "Basic",
            "Cache-Control": "no-cache"
        }
    }).done(function() {
        alert("Success.");
    }).fail(function() {
        alert("Sorry. Server unavailable. ");
    });
}
  • 写回答

1条回答 默认 最新

  • weixin_33701564 2018-01-17 15:23
    关注

    If you're getting a 200 result from the Ajax request then it's failing when it's attempting to parse the response.

    Change the dataType value in the Ajax request to "json". It's currently "application/json", which is not a valid data type...

    function data() {
        $.ajax({
            type: "GET",
            url: "url",
            dataType: "json",    //  make sure this is correct
            headers: {
                "Authorization": "Basic",
                "Cache-Control": "no-cache"
            }
        }).done(function() {
            alert("Success.");
        }).fail(function() {
            alert("Sorry. Server unavailable. ");
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真