在firefox中,当为CORS获取数据时,我们只能使用json或脚本,但如果无法获取数据,则显示语法错误


In firefox while fetching the data for CORS We can use only jsonp or script but if cant get data then it shows syntax error

php代码

<?php
$data = trim(api_get('threads',array(),'JSON'));
echo $_GET['jsonp_callback'] . '(' . $data . ');';
?>
<<p> js代码/strong>
<script>
$.ajax({
              url: "https://abc.secure.tttt.com/",
              data:{},
              dataType: 'jsonp',
              cache: false,
              crossDomain: true,
              jsonp : "jsonp_callback",
              success: function(params){
                    console.log('showInbox');
                    //console.log(JSON.parse(params));
                    showInbox(params);
              },
              error: function (xhr, textStatus, thrownError) {
                console.log('Inside showInbox ajax failure');
                console.log(xhr.status);
                console.log(textStatus);
                console.log(JSON.stringify(xhr));
                console.log(thrownError);
              }

});

这个工作很好,如果API给出json数据,如果有一个错误,它显示语法错误:

Timestamp: 5/9/2013 4:28:24 PM
Error: SyntaxError: syntax error
Source File: https://abc.secure.tttt.com/?jsonp_callback=jsonp1368095332316&_=1368097103961
Line: 1
Source Code:
<!DOCTYPE html>

使用timeout属性获取出错时执行的错误函数

$.ajax({
              timeout : 2000,
              url: "https://abc.secure.tttt.com/",
              data:{},
              dataType: 'jsonp',
              cache: false,
              crossDomain: true,
              jsonp : "jsonp_callback",
              success: function(params){
                    console.log('showInbox');
                    //console.log(JSON.parse(params));
                    showInbox(params);
              },
              error: function (xhr, textStatus, thrownError) {
                console.log('Inside showInbox ajax failure');
                console.log(xhr.status);
                console.log(textStatus);
                console.log(JSON.stringify(xhr));
                console.log(thrownError);
              }

});

在大多数浏览器中,除了超时之外,没有办法告诉JSONP请求是否成功。当然,将其设置为一个合理的值,我用2秒的时间猜测,如果服务器在此时间内没有返回文件,则有问题。