作者pttnews (PTT新聞)
看板Ajax
標題[問題] (已解決) Jquery的AJAX回傳TEXT
時間Wed Feb 27 17:29:38 2013
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
1F:→ Darkland:猜你測試網頁直接從本機開,這樣會有 cross-domain 的狀 02/27 21:06
2F:→ Darkland:建議你開瀏覽器的開發者工具來看 02/27 21:17
我有用FireFox + FireBug , 可是js trace 到Jquery 我就暈了,看不懂
哭哭!
※ 編輯: pttnews 來自: 220.135.143.146 (02/27 22:01)
3F:→ kerash:content type 改 text/html 或 text/plain 02/27 23:22
4F:→ mervynW:看consolec回應的ajax內容, http header 02/28 13:18
已解決!
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)
5F:推 mrbigmouth:127.0.0.1跟localhost是不同的網域..... 03/01 11:27
6F:→ mrbigmouth:當然會有cross-domain的問題 03/01 11:27
7F:→ mrbigmouth:jQuery的data-type設為script時遇到不同網域應該是自動 03/01 11:28
8F:→ mrbigmouth:幫你以jsonp的方式...所以可以跨網域取資料... 03/01 11:28
但是我用data-type : text,所以.... 也算跨網域?
※ 編輯: pttnews 來自: 59.120.175.188 (03/01 11:31)
9F:推 mrbigmouth:只有javascript可以跨網域 03/01 11:50
10F:→ mrbigmouth:你用127.0.0.1去取localhost的資料(或者反過來)就是跨 03/01 11:50
11F:→ mrbigmouth:網域...會有安全性限制 只有json資料或javascript可以 03/01 11:51
12F:→ mrbigmouth:靠script src的方式去繞過這一限制 詳情請自己google 03/01 11:51
13F:→ mrbigmouth:same domain policy jsonp 03/01 11:51
我懂了~非常感謝
謝謝您
※ 編輯: pttnews 來自: 59.120.175.188 (03/01 13:54)