|
 |
|
jquery มีปัญหากับการดึงข้อมูล xml จากไฟล์ php ไม่ได้ครับ |
|
 |
|
|
 |
 |
|
Code (JavaScript)
$.ajax({
url: "http://www.bac.com/aaa/playlist.php",
dataType: "xml",
async: false,
success:function(xml){
console.log('sss '+xml);
$(xml).find("channel").each(function(){
console.log('txt: '+$(this).find('title').text());
});
},
error: function(jqXHR, textStatus, ex) {
alert(textStatus + "," + ex + "," + jqXHR.responseText);
}
});
ผมลองใช้ firebug เช็คดู มันเห็นไฟล์ .php นะครับ แล้วมันก็ฟ้องในแท็บ xml ว่า
การแจกแจงเอกสาร XML ผิดพลาด : ไม่พบเอลิเมนท์ ที่ตั้ง : moz-nullprincipal:{123} บรรทัดที่ 1, คอลัมน์ 1 :
ใน console ของ chorm มันขึ้นแบบนี้ครับ
XMLHttpRequest cannot load http://www.bac.com/aaa/playlist.php. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
Response Header
Code
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection Keep-Alive
Content-Encoding gzip
Content-Length 439
Content-Type text/xml; charset=UTF-8
Date Mon, 10 Jun 2013 09:39:30 GMT
Keep-Alive timeout=1, max=100
Server Apache/2
Vary Accept-Encoding,User-Agent
X-Powered-By PHP/5.2.17
Request Header
Code
Accept application/xml, text/xml, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language th-th,th;q=0.8,en-us;q=0.6,en-gb;q=0.4,en;q=0.2
Host www.abc.com
Origin http://localhost
Referer http://www.bac.com/aaa/playlist.php
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
รบกวนทีนะครับ
ขอบคุณครับ
Tag : PHP, jQuery

|
|
 |
 |
 |
 |
Date :
2013-06-10 16:50:26 |
By :
fogza |
View :
1538 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
XMLHTTPRequest ไม่สามารถ request ข้าม host ไม่ได้ครับ
จะติดเรื่องของ Access-Control-Allow-Origin นั่นล่ะครับ
|
 |
 |
 |
 |
Date :
2013-06-10 16:53:19 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้การทำ proxy เอาครับ
Code สมมติว่าชื่อ proxyXML.php (PHP)
<?php
echo file_get_content('http://www.bac.com/aaa/playlist.php');
?>
แล้วแทนที่จะให้ ajax ที่ http://www.bac.com/aaa/playlist.php โดยตรง ก็ให้โฮสต์เราอ่านมาจากเว็บเค้า แล้วให้ jQuery เรียกผ่านตัวนี้มา จะสามารถเลี่ยงปัญหานี้ได้ครับ
แต่ทั้งนี้ แนะนำให้ทำ cache ที่ฝั่งเซิร์ฟเวอร์เพื่อประหยัดแบนด์วิธด้วยนะครับ
|
 |
 |
 |
 |
Date :
2013-06-10 17:17:26 |
By :
itpcc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามนั้นเลยครับ เพราะทางฝั่งเซิร์ฟเวอร์จะไม่ติดตรงนี้
|
 |
 |
 |
 |
Date :
2013-06-10 17:23:06 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|