Firefox和Chrome,Opera上的jQuery Ajax问题(在Safari上运行良好)

问题描述:

我有一个使用jQuery的HTML代码。在我的代码中,我调用了一个简单的ajax调用。它在Safari上运行良好,但在Firefox和Chrome上运行良好。Firefox和Chrome,Opera上的jQuery Ajax问题(在Safari上运行良好)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 

<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>untitled</title> 
    <meta name="generator" content="TextMate http://macromates.com/"> 
    <meta name="author" content="Daniel"> 
    <!-- Date: 2011-11-07 --> 

    <script type='text/javascript' src='http://media.smashingmagazine.com/themes/smashing/js/jquery-1.5.2.min.js?ver=3.1.3'></script> 
    <script type='text/javascript' src='http://media.smashingmagazine.com/themes/smashing/js/jquery-ui-1.8.2-min.js?ver=3.1.3'></script> 
</head> 

<script type="text/javascript"> 
    var jwt = ""; 
    var certUrl = ""; 

    function getJwt() { 
     $.get(
      "http://signerapp.appspot.com/signerapp", 
      function(data) { jwt = data;} 
     ); 
    } 
    function checkAvailable() { 
     var postObject = {}; 
     postObject.certUrl = certUrl; 
     postObject.jwt = jwt; 
     postObject.action = "available"; 
     postObject.shortUrl = $('#shortUrl').attr('value'); 
     $.ajax({ 
        url: "http://clickin-shorturl.appspot.com/urlshortener/v1/url", 
        type: "POST", 
        data: JSON.stringify(postObject), 
        success: function(data) { alert(data); } 
       }); 
    } 
    $(document).ready(function() { 
     getJwt(); 
    }); 
</script> 
<body> 
</br> 
</br> 
</br> 
</br> 
</br> 
<h4>This is sample for creating url shortener</h4> 
<p>Check available:</p> 
<div> 
    <!-- Check availability--> 
<table border="1" width="540" height="100%"> 
    <!-- Check availability--> 
    <tr> 
     <td> 
      <form> 
       Action: <input type="textbox" id="action1" name="action1" value="available" disabled="disabled" size="20"></br> 
       Short URL: <input type="textbox" id="shortUrl" name="shortUrl" value="aaaa" size="20"></br> 
       <input type="button" id="checkButton" name="checkButton" value="Check Available" onClick="checkAvailable();"></br> 
      </form> 
     </td> 
    </tr> 
    <tr> 
</table> 

</div> 

</body> 
</html> 

问题在于调用上面的$ .get和$ .ajax。 使用Safari,我得到了准确的结果:$ .get的base64字符串和$ .ajax的json 但是对于Firefox,我无法收到任何东西虽然响应代码是“200 OK”,但主体确实包含任何东西。

您可以使用Firefox的萤火或Chrome的检查元素以查找有关错误

这是因为same origine policy的更多信息。您无法使用ajax请求加载其他网站内容。如果你想这样做,你必须为此使用JSONP。或者你可以使用服务器端代理。