我收到以下错误:jquery ajax readystate 0 responsetext status 0 statustext error给它时: url(http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm)但是当我url(localhost:""/embparse_page)在我的localhost上给它时,它工作正常.
我曾尝试使用我在Google搜索中找到的标题,我也使用beforeSend:""了它,但它仍然无效.
我认为主要问题是:XMLHttpRequest cannot load http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm. Origin "local server" is not allowed by Access-Control-Allow-Origin.但我不明白.
任何人都可以向我解释这个问题,因为我对此很陌生.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:ng="http://angularjs.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta Access-Control-Allow-Origin="*" />
<title>Page Parsing</title>
<script type="text/javascript" src="/js/jquery-1.9.1.min.js"></script>
<script>
getit=function(){
jQuery.support.cors = true;
$.ajax({
type:"GET",
url:"http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm",
dataType:"html",
crossDomain:true,
beforeSend: function(xhr) {
xhr.overrideMimeType('text/plain;charset=UTF-8');
},
success:function(XMLHttpRequest,jqXHR ,data) {
//alert(data.title);
var starttitl=data.lastIndexOf('<title>');
var endtitl=data.lastIndexOf('</title>'); …Run Code Online (Sandbox Code Playgroud) 我读完所有这些类似的问题:1,2,3,4,5和6.但我需要调试更多.
所有这些问题都表明问题是由跨域政策引起的并且e.preventDefault()解决了问题.但我怀疑这可以打破其他一些事情,所以我需要确定.
在我的网站上,我有一个每天被发射15k次的操作.当用户访问网页时,我的javascript文件会检查一个元素.如果元素存在,则进行AJAX调用.
$(document).ready(function() {
// I do something here
if ($('#mydiv').length !== 0) {
var startTime = new Date().getTime();
$.ajax({
type: "POST",
url: "/mypage",
success: function (data) {
// I do something here
},
timeout: 20000,
error: function(r, s, e) {
var elapsedTime = (new Date().getTime() - startTime );
var string = "readyState: "+r.readyState+", status: "+r.status+", statusText: "+r.statusText+", responseText: "+r.responseText+", textStatus: "+s+", error: "+e +", …Run Code Online (Sandbox Code Playgroud) 我无法使用jquery的ajax功能成功发布.
运行页面的http://localhost:9999URL是,目标(Web服务)的URL是http://localhost:8080.没有端口不相同,它们分别是9999和8080.
下面是请求和jquery ajax代码.
请求:
OPTIONS /profile/set_member HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost:9999
Access-Control-Request-Method: POST
Run Code Online (Sandbox Code Playgroud)
jQuery ajax代码:
$.ajax({
type: "POST", url: "http://localhost:8080/profile/set_member",
contentType: "application/json", data: member,
error: function(){ alert('Update failed!'); },
processData: false,
success: function(){ alert('Update successful!'); }
});
Run Code Online (Sandbox Code Playgroud)