2012-04-04 139 views
0

我試圖使用ajax訪問其餘服務。
如果我會貼在任何網頁瀏覽器這個鏈接就會給這樣的結果:

{"SessionID":"a7f58a4a-f922-47c1-8351-d2035df4968c","SourceIP":"127.0.0.1","UserID":313} 

(鏈接已更改爲安全)
https://thisisjustasample/Rest/Authenticate/Login?username=user&password=pass123&ip=127.0.0.1

但是我發現沒有運氣訪問鏈接用ajax:

從 '查看' 部分的呼叫:

getdata('https://thisisjustasample/Rest/Authenticate/Login?username=user&password=pass123&ip=127.0.0.1');  

js文件中的函數:

function getdata(url) { 
    $.ajax({ 
     url: url, 
     type: 'GET', 
     contentType: 'application/json', 
     success: function (data) { 
      if (data) { 
       showtooltip(data); 
      } 
     }, 
     error: function (xhr, ajaxOptions, error) { 
      showtooltip(xhr.status + ': ' + error); 
     } 
    }); 
} 

它總是返回'0'狀態。當我檢查我的數據庫時,數據沒有任何改變。代碼有什麼問題?

+0

你在'firebug'中看到了什麼? – gdoron 2012-04-04 09:20:12

+2

你確定你沒有進行跨域請求嗎? – 2012-04-04 09:20:53

+0

我不熟悉螢火蟲,但我會嘗試使用它。 – fiberOptics 2012-04-04 09:21:23

回答

0

我有一種感覺,問題是,當你不應該指定一個contentType時,你指定了一個json的內容類型,你的params應該是數據的一部分。

$.ajax({ 
    url: "https://thisisjustasample/Rest/Authenticate/Login", 
    type: 'GET', 
    data: "username=user&password=pass123&ip=127.0.0.1", 
    success: function (data) { 
     if (data) { 
      showtooltip(data); 
     } 
    }, 
    error: function (xhr, ajaxOptions, error) { 
     showtooltip(xhr.status + ': ' + error); 
    } 
}); 
+0

沒有工作。 :( – fiberOptics 2012-04-04 09:34:12

+2

如果此JavaScript運行在http:// ** url1 ** /some.html頁面上,並且ajax看起來像url:「https:// ** url2 **/Rets ...,does * * url1 ** = ** url2 **? – 2012-04-04 09:36:56

+0

您需要使用fiddler或Firebug或其他軟件來監控請求和響應,否則您的編碼是盲目的,您還需要知道跨域調用的含義,如果您是打不開,我不認爲這是你的問題,但你需要確定 – suing 2012-04-04 11:39:42