[英]AJAX-Get data from php
我是AJAX的新手。任務是我必須從php文件中獲取數據並將其存儲在javascript變量中。 我已經通過了很多例子,但沒有找到幫助。 我在這里給出一個偽html代碼:
<html>
<head>
<script>
function ajaxfunction()
{
//code for httprequest
**call the php file declare a variable and store the response of php**
//return the variable
}
</script>
</head>
<body>
//my code for displaying a map
**mainvariable=ajaxfunction();//storing the value of subvariable(data from php) in mainvariable**
//use the mainvariable and do the remaining task
<body>
我的PHP代碼:
<?php
$file=fopen("datapoints.txt","r");
$read=fread($file,filesize("datapoints.txt"));
fclose($file);
echo $read;
?>
這里的問題是我在調用php文件時使用的html文件中沒有任何表單變量。 只是當頁面加載時,應該調用“ajaxfunction()”並從php獲取數據並存儲在變量................
我想你可以理解我的問題
任何幫助是極大的贊賞
你可以在這里使用jQuery。 這里的文檔是http://api.jquery.com/jQuery.ajax/ 。
一個例子如下:
<html>
<head>
<!-- Include jquery from Google here -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
// Wait till dom is loaded
$(document).ready(function() {
// When id with Action is clicked
$("#Action").click(function()
{
// Load ajax.php as JSON and assign to the data variable
$.getJSON('ajax.php', function(data) {
// set the html content of the id myThing to the value contained in data
$("#myThing").html(data.value);
});
});
});
</script>
</head>
<body>
<a id="Action">Click Me</a>
<p id="myThing"></p>
</body>
</html>
您的ajax.php文件只能包含:
<?php
echo json_encode(array("value" => "Hello World"));
?>
如果您想將數據從php發送到javascript,可以使用json_encode。 要使用php接收數據,你可以使用$ _GET和$ _POST(只要你正在編寫一個簡單的應用程序):)
對於你的Ajax請求你(我只允許這個問題的作者)可以使用我的javascript代碼:
function getRequestObject(){
var o = null;
if(window.XMLHttpRequest){
o = new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
o = new ActiveXObject('Msxml2.XMLHTTP');
}catch(e1){
try{
o = new ActiveXObject('Microsoft.XMLHTTP');
}catch(e2){
}
}
}
return o;
}
function request(method, adress,sendData,callback){
var o = getRequestObject();
var async = (callback!==null);
if(method === 'GET'){
if(sendData!=null){adress+="?"+sendData;}
o.open(method, adress, async);
o.send(null);
}else if(method === 'POST'){
o.open(method, adress, async);
o.setRequestHeader('Content-Type' , 'application/x-www-form-urlencoded');
o.send(sendData);
}
if(async){
o.onreadystatechange = function (){
if(o.readyState==4&&o.status==200){
callback(o.responseText);
}else if(o.readyState==4&&o.status!=200){
//Error
}
};
}
if(async){return ;}
else{return o.responseText;}
}
RequestCache實現為Object(看看這是如何在Javascript中完成的)但也許JQuery左右也可以解決你的任務。
function RequestCache (){}
RequestCache.cache = Array();
RequestCache.getRequest=function (method, adress,sendData,callback,enforceReload){
if(enforceReload===null){enforceReload=false;}
var url = method+adress+sendData;
var newUrl = true;
if(typeof(enforceReload)==="undefined"||enforceReload===false){
for(var key in RequestCache.cache){
if(key===url){
newUrl=false;
break;
}
}
}
if(newUrl){
if(callback){
request(method, adress,sendData,
function(res){
RequestCache.cache[url]=res;
callback(res);
}
);
}else{
RequestCache.cache[url]=request(method, adress,sendData,null);
return RequestCache.cache[url];
}
}else{
if(callback){
callback(RequestCache.cache[url]);
}else{
return RequestCache.cache[url];
}
}
};
RequestCache.setRequest = function (method, adress,sendData,result){
var url = method+adress+sendData;
RequestCache.cache[url] = result;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.