作者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)