[問題] (已解決但很奇怪)Jquery 1.7的AJAX回傳TEXT

看板Ajax作者 (PTT新聞)時間12年前 (2013/02/27 17:29), 編輯推噓2(2011)
留言13則, 4人參與, 最新討論串1/1
getArray.php header('Content-type: application/text; charset=utf-8'); echo "test,123,test,456"; javascript var jqxhr = $.ajax({ url: "http://localhost/getArray.php", dataType : "text" }) .success(function(response, status) { alert("OK" + response ); }) .error(function(jqXHR, textStatus, errorThrown) {alert("err");}) 當我的dataType 設為 text 會呼叫error 當我的dataType 設為 script 呼叫sucess ,並出現OKundefined 我用jquery 1.7, 我想要用text作為dataType 請問上述程式哪裡有錯呢? 搞了一天快瘋了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.175.188

02/27 21:06, , 1F
猜你測試網頁直接從本機開,這樣會有 cross-domain 的狀
02/27 21:06, 1F

02/27 21:17, , 2F
建議你開瀏覽器的開發者工具來看
02/27 21:17, 2F
我有用FireFox + FireBug , 可是js trace 到Jquery 我就暈了,看不懂 哭哭! ※ 編輯: pttnews 來自: 220.135.143.146 (02/27 22:01)

02/27 23:22, , 3F
content type 改 text/html 或 text/plain
02/27 23:22, 3F

02/28 13:18, , 4F
看consolec回應的ajax內容, http header
02/28 13:18, 4F
已解決! js 這一段 url: "http://localhost/getArray.php" 為了讓大家不要眼花, 其實在程式是這樣寫的 url: "<?php echo 'http://' . $_SERVER['SERVER_ADDR'] . ':' . $_SERVER['SERVER_PORT'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') ;?>" + "/getArray.php", 如果改成 url: "<?php echo 'http://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') ;?>" + "/getArray.php", 就一切妥當了。 真是奇怪~ url: "http://localhost/getArray.php"跟 url: "http://127.0.0.1/getArray.php" 為什麼會影響Jquery ,不都是一樣嗎? 為了一行,花了一天,見鬼了 ※ 編輯: pttnews 來自: 59.120.175.188 (03/01 11:17)

03/01 11:27, , 5F
127.0.0.1跟localhost是不同的網域.....
03/01 11:27, 5F

03/01 11:27, , 6F
當然會有cross-domain的問題
03/01 11:27, 6F

03/01 11:28, , 7F
jQuery的data-type設為script時遇到不同網域應該是自動
03/01 11:28, 7F

03/01 11:28, , 8F
幫你以jsonp的方式...所以可以跨網域取資料...
03/01 11:28, 8F
但是我用data-type : text,所以.... 也算跨網域? ※ 編輯: pttnews 來自: 59.120.175.188 (03/01 11:31)

03/01 11:50, , 9F
只有javascript可以跨網域
03/01 11:50, 9F

03/01 11:50, , 10F
你用127.0.0.1去取localhost的資料(或者反過來)就是跨
03/01 11:50, 10F

03/01 11:51, , 11F
網域...會有安全性限制 只有json資料或javascript可以
03/01 11:51, 11F

03/01 11:51, , 12F
靠script src的方式去繞過這一限制 詳情請自己google
03/01 11:51, 12F

03/01 11:51, , 13F
same domain policy jsonp
03/01 11:51, 13F
我懂了~非常感謝 謝謝您 ※ 編輯: pttnews 來自: 59.120.175.188 (03/01 13:54)
文章代碼(AID): #1HBT87BL (Ajax)
文章代碼(AID): #1HBT87BL (Ajax)