jqueryでajaxの送信の際通信状況を見ようと思い下記のようにコードを書いたのですが
jquery-1.8.3.js:8434 OPTIONS url 404 (Not Found)
Failed to load url: Response for preflight does not have HTTP ok status.
というエラーが返ってきます。どうしたらよろしいでしょうか
jquery
1$.ajax({ 2 cache: false 3 async: true 4 ,xhr : function() 5 { 6 XHR = $.ajaxSettings.xhr(); 7 if(XHR.upload) 8 { 9 XHR.upload.addEventListener('progress',function(e){ 10 var percent = parseInt(e.loaded / e.total * 10000) /100; 11 console.log(e.loaded,e.total); 12 console.log(percent); 13 $('#progressBar').css("width",percent + "%"); 14 }, false); 15 } 16 return XHR; 17 } 18 ,url: url + vkey 19 ,type: 'post' 20 ,data : fd 21 ,processData: false 22 ,contentType: false 23 24 }).done(function(data, textStatus, jqXHR){ 25 alert("secsses"); 26 27 28 }).fail(function(jqXHR, textStatus, errorThrown){ 29 alert("failllllllllll"); 30 });
##追記
jquery
1$.ajax({ 2url: url + vkey 3 ,type: 'post' 4 ,data : fd 5 ,processData: false 6 ,contentType: false 7 8 }).done(function(data, textStatus, jqXHR){ 9 alert("secsses"); 10 11 12 }).fail(function(jqXHR, textStatus, errorThrown){ 13 alert("failllllllllll"); 14 });
これだとうまく行きました。ただ送ってるだけですけど
##追記2
ご指摘の通りCORSを使用したリクエストなのでクライアント側とサーバー側を編集いたしました。
クライアント側ではwithCredentialsオプションをtrueにして送信する。
サーバ側はヘッダーにAccess-Control-Allow-Originを正しくセットするのとAccess-Control-Allow-Credentialsをtrueにする。
jquery
1$.ajax({ 2// cache: false 3 async: true 4 ,xhr : function() 5 { 6 XHR = $.ajaxSettings.xhr(); 7 if(XHR.upload) 8 { 9 XHR.upload.addEventListener('progress',function(e){ 10 var percent = parseInt(e.loaded / e.total * 10000) /100; 11 console.log(e.loaded,e.total); 12 console.log(percent); 13 $('#progressBar').css("width",percent + "%"); 14 }, false); 15 } 16 return XHR; 17 } 18 ,url: url + vkey 19 ,xhrFields: { 20 withCredentials: true 21 } 22 ,type: 'post' 23 ,data : fd 24 ,processData: false 25 ,contentType: false 26 27 }).done(function(data, textStatus, jqXHR){ 28 alert("secsses"); 29 30 31 }).fail(function(jqXHR, textStatus, errorThrown){ 32 alert("failllllllllll"); 33 });
しかしそれでもエラー内容は変わらずエラーになってしまいます
