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.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?