Jul
9
2009
9
2009
jQuery and Ajax Cross-Site
วันนี้เราจะมาแนะนำการทำ ajax โดยใช้ jQuery แบบ Cross-Site กันครับ
การทำงาน ผมจะใช้ ajax ไปโหลด page url มาซึ่งทาง server-side ผมใช้ PHP ครับและ ทาง PHP ก็ return ค่ากลับมาเป็น json ครับ
Code ในส่วนของ Html
<div style="background-color:#333333;height:20px;"> <a id="top" href="javascript:void(0);" gt;Show Message</a> </div> <div id="layDiv" style="display:none;height:30px;font-weight:bold;color:#ff3300;"></div>
Code ในส่วนของ Javascript
<script language="javascript">
$("document").ready(function(){
$("#top").click(function(){
jQuery.ajax({
url: 'http://executedns.net/hello.php',
type: 'GET',
dataType: 'jsonp',
dataCharset: 'jsonp',
success: function (data){
var content = data.msg;
alert(content);
$('#layDiv').html('');
$('#layDiv').html(content);
$('#layDiv').css("display","block");
}
});
});
});
</script>
สิ่งที่สำคัญสำหรับ code ในส่วนนี้ก็จะเป็น parameter dataType and dataCharset จะต้องเป็น jsonp ครับ
และนี่ก็เป็น Code ในส่วนของ PHP
<?php
$val = array("msg"=>"Hello World !");
echo $_GET['callback'].'('.json_encode($val).')';
?>
Demo
Related Posts
Tags
.data
access iframe
advance
Ajax
attr
bind
browser
canvas
Core
debugs
Deferred
deferred.then
Deferred Object
delegate
DOM
Effect
event
Events
filters
google
HTML
html5
iframe
Image
jquery
jquery.data
jQuery 1.4
jquery 1.5
jQuery 1.6
jQuery 1.7
jquery mobile
jquery plugin
jquery ui
json
live
Plugin
plugins
selector
Selectors
show()
UI
undelegate
validate
welcome
what's jquery
Facebook Development
Google+ Page
Categories
- HTML5 & CSS3 (8)
- jQuery 1.4 (9)
- jQuery 1.5 (6)
- .data (1)
- jQuery 1.6 (3)
- .data (1)
- jQuery 1.7 (4)
- Events (1)
- jQuery Mobile (3)
- jQuery UI (3)
- Mobile (1)
- News (8)
- Others (28)
- Plugin (15)
- The basics of jQuery (9)
- Tips (13)
- คู่มือการใช้งาน jQuery ฉบับ ภาษา ไทย (30)
- Ajax (4)
- Attributes (1)
- Core (1)
- Deferred (3)
- Effects (7)
- Events (3)
- Manipulation (2)
- Selectors (5)
- Traversing (2)
- Utilities (2)

An article by







