28/7/57

jquery ใช้ ajax เพื่อเรียกค่าผ่าน webservice บน host

jquery ใช้ ajax เพื่อเรียกค่าผ่าน webservice บน host เรียกค่าบนเว็บ host ดึงค่าลงมาเพื่อทำค่าต่อไป








ไฟล์แรก test.php
<html>
<head>
<title>test</title>
</head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<!-- Javascript -->
<script type="text/javascript">
$(document).ready(function (){
var url = "http://vissarud.com/showblog/1234.php";
$.ajax({
   type : "GET",
   url : url, data : {},
   dataType : "jsonp",
   error : function(jqXHR, textStatus, errorThrown) {alert(errorThrown); alert(textStatus);},

   success : function(data) { $.each(data, function(index, element) {
      alert(element.ID);
      alert(element.Type);
      alert(element.Text);
   }); } }); });
</script>
<body>
</body>
</html>

ไฟล์ที่สอง(เอาไว้บนเว็บ) 1234.php
<?
header('Content-Type: application/json');
echo $_REQUEST['callback'].'('.'{"Announcement":{"ID":1,"Type":1,"Text":"This is a test Albums announcement.","TimeStart":"1969-12-31","TimeEnd":"1969-12-31"}}'.')';
?>

ขอบคุณ http://www.pureexample.com/jquery/cross-domain-ajax.html http://stackoverflow.com/questions/14621356/error-jquery1830649454693285679-1359620502896-was-not-called-json