douzhong1730 2011-02-09 14:08
浏览 55
已采纳

Jquery getJSON抛出:在Safari中“无法加载资源:已取消”

I've found a few posts (here and across the web) on this issue and tried the proposed solutions with no success, but there were some differences in the original issue. My example seems very straightforward, so I'd love to get some input on this. Here's the code:

theURI = "https://<?= $_SERVER['HTTP_HOST'] ?>/validate.php";
theData = {
    'validationType' : 'login',
    'ident' : document.getElementById("login_ident").value,
    'password' : document.getElementById("login_pw").value,
    'loginTries' : <?= $loginTries ?>
}
$.getJSON(theURI, theData, function() {alert('Success!')});

The "Success!" alert shows up fine but the error "Failed to load resource: cancelled" flashes through the error console. "validate.php" is as follows (some of the server code has been omitted for clarity), assume the variables are all basically valid values:

<?php
echo '{';
echo '"result" : "' . $login_success . '",';
echo '"loginTries" : "' . $loginTries . '",';
echo '"feedback" : "' . $loginFeedback . '",';
echo '"feedbackClass" : "' . $loginTextClass . '",';
if($login_success) {
    echo '"userData" : {';
    $firstPass=true;
    foreach ($g_aUserData as $theKey => $theValue) {
        if(!$firstPass) echo ","; else $firstPass=false;
        echo '"' . $theKey . '" : "' . $theValue . '"';
    }
    echo '}';
}
echo '}';
?>

Which generates (sample data, of course):


{"result" : "1","loginTries" : "0","feedback" : "SUCCESSFUL LOGIN!","feedbackClass" : "login_text","userData" : {"firstName" : "Gomez","memberID" : "39203","lastName" : "Aadams","lastLogin" : "","message" : "","credHash" : "3baf7a67271ecfd93kdsid33s2a65c16a9413f652"}}

Which reformats to:

{
    "result" : "1",
    "loginTries" : "0",
    "feedback" : "SUCCESSFUL LOGIN!",
    "feedbackClass" : "login_text",
    "userData" : {
        "firstName" : "Gomez",
        "memberID" : "39203",
        "lastName" : "Aadams",
        "lastLogin" : "",
        "message" : "",
        "credHash" : "3baf7a67271ecfd93kdsid33s2a65c16a9413f652"
    }
}

Any ideas? I've tried:

1) Loading the data into a querystring and omitting the data param entirely.
2) Changing the call to be relative.
3) Changing the "https" to "http".

UPDATE: OK. Based on comments, I paid my stupid tax and had the code generate the JSON data without any HTML trimmings. Strangely, I still am getting the same error. Tried the same fixes noted above, still not working.

  • 写回答

1条回答 默认 最新

  • doutou7740 2011-02-09 22:32
    关注

    So, it turns out that the problem had naught to do with the ajax call, but with the form (that had been converted to Ajax) that was being used for the inputs. Once I added "return false" to the form, everything cleared up.

    But I must say thanks to lonesomeday for reminding me just how AJAX works (it'd been awhile since I've coded.) Over the course of prepping the question initially, I was able to dig out some other errors before the post, so all-in-all, simply asking the question helped immensely.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料