2010-08-11 128 views
0

heh,
我正在使用jQuery AJAX調用從自己託管的Web服務(同一個域)中提取數據,但它始終返回0,這表示跨域問題。但這不應該是一個問題。違反了同源策略?

任何建議如何解決這個問題?謝謝!

網站上運行我的腳本

http://www.mysite.com/facebook/el_login 

我的AJAX調用:

var data = 'username=' + username.val() + '&password=' + password.val() 
$.ajax({ 
      url: "http://www.mysite.com/api/v01/account/exists.json", 
      type: "GET",   
      data: data,  
      cache: false, 
      complete: function(transport) { 
       if(transport.status == 200) { 
        alert('Success'); 
       } else { 
        alert('Failed ' + transport.status); 
       } 
      } 

     }); 
    }) 

Firebug的請求報頭:

Request Headersview source 
Host www.mysite.com 
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 
Accept */* 
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 
Proxy-Connection keep-alive 
Content-Type application/x-www-form-urlencoded 
X-Requested-With XMLHttpRequest 
Referer http://www.mysite.com/facebook/el_login 
Cookie sessionid=xxx 

編輯:

好吧,似乎AJAX調用靜態網站(同一臺服務器)正在工作。我的Webservice後端基於Django,Apache2和mod_wsgi ..也許這是失敗的原因。

+1

看起來不錯。你確定你總是使用'http://'而不是將它與'https://'混合在一起?沒有不同的端口?沒有自動刪除或添加'www.'? – 2010-08-11 11:31:37

+1

您是否嘗試在回調中添加'function(transport,textStatus)'並查看'textStatus'是否提供了更詳細的信息? – 2010-08-11 11:32:49

+0

textStatus返回null,表示中止的請求。 – 2010-08-11 11:35:49

回答

1

好了,幾個小時後的星際爭霸2發現了它。
我有線我的提交按鈕與

$('#submit').click(function() 

這似乎創造了一些問題與jQuery的Ajax

解決方案:
使用「舊」風格線了你的Ajax調用。

<input type="button" value="Submit" id="submit" onClick="sendLogin()" /> 

function sendLogin() { 
    var query_data = { username: 'test', password: 'test'}; 

    $.ajax({ 
      url: "http://www.mysite.com/api/v01/account/exists.json", 
      type: "POST",   
      data: $.toJSON(query_data),  
      dataType: 'application/json', 
      contentType: 'application/json', 
      complete: function(transport) { 
      if(transport.status == 200) { 
        alert('Success'); 
       } else { 
        alert('Failed ' + transport.status); 
       } 
      } 
     }); 
} 
+0

奇數。如果你使用'$('#formID')。submit(function ...'並且改變輸入類型以提交(並返回false)? – Jhong 2010-08-12 08:23:52

1

對於debuggin我用:

function showResponse(responseText, statusText, xhr, $form) { 
    debugger; // TODO: Remove in production. 
} 

,並在Ajax調用使用

error: showResponse 

您是否嘗試過使用下列參數:

 dataType: "json", 
     contentType: "application/json; charset=utf-8", 
+0

我的webservice不需要內容類型,如果我提供一個查詢字符串 – 2010-08-11 11:52:10

+0

,但在您的示例中,我沒有看到url參數中的查詢字符串... – 2010-08-11 11:54:06

+0

他將「數據」屬性中的對象傳遞給jQuery – Pointy 2010-08-11 12:26:34