---
title: "Exempel med ajax"
type: "forum-thread"
url: "https://www.webforum.nu/amne/javascript/185076-exempel-med-ajax"
topic: "JavaScript"
topic_url: "https://www.webforum.nu/amne/javascript"
author: "md7dani"
published: "2010-10-30T08:51:08.000Z"
updated: "2010-10-30T10:52:28.000Z"
replies: 2
views: 503
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/javascript/185076-exempel-med-ajax"
---

# Exempel med ajax

## #1 — md7dani, 2010-10-30T08:51Z

Får inte exemplet att fungera och förstår inte varför. Jag är helt ny med det här så kan ha missat nåt fundamentalt. Koden nedan:

```
<?php  // Error Handling ini_set('display_errors',1); 
error_reporting(E_ALL & ~E_NOTICE); 
// BACKEND 
if( isset($_GET['ajax']) ) {  
echo date('l dS \of F Y h:i:s A');  
exit(); } ?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset:utf-8"> 
<title>Scheduled ajax Example</title> 
<script type="text/javascript" src="/yui/build/yahoo-dom-event/yahoo-dom-event.js"></script> 
<script type="text/javascript" src="/yui/build/connection/connection-min.js"></script> 
<script type="text/javascript"> 
// My namespace (to avoid conflicts with global variables) 
FOTI = window.FOTI ¦¦ {}; 
// My page specific object 
FOTI.ScheduledajaxExample = function() {  return {   
/**    * (psuedo) Private method for connecting.    */   
_connect : function(method,uri,callback,postdata){    
YAHOO.util.Connect.asyncRequest(method,uri,callback,postdata);   
},   
/**    * Node used to store the results    
*/   resultsNode : null,   /**    * A counter, just for testing    */   counter : 0,   /**    * Initialization method    */   init : function(e) {    
// Get the results node    
FOTI.ScheduledajaxExample.resultsNode = YAHOO.util.Dom.get('results');    
if( FOTI.ScheduledajaxExample.resultsNode!= null ) {     
// Start the schedule     
setTimeout( FOTI.ScheduledajaxExample.connect, 3000 );    }   },   
/**    * Connect to server    */   
connect : function() {    
// ajax connection stuff goes here    
var uri = 'scheduledajax.php?ajax=true&nocache=' + (new Date()).getTime();    
var timeout = 2; // seconds    
var callback = {     success : function(o){      
FOTI.ScheduledajaxExample.resultsNode.innerHTML = "<div>" + (FOTI.ScheduledajaxExample.counter++) + " : " + o.responseText + "</div>";      
setTimeout( FOTI.ScheduledajaxExample.connect, 3000 );     },     
failure : function(){      
alert('Connection failed');     },     timeout : timeout * 1000    };    FOTI.ScheduledajaxExample._connect('GET', uri, callback, null );      }  }; }(); 
// Fire my methods once the page loads 
YAHOO.util.Event.on(window,'load',FOTI.ScheduledajaxExample.init); 
</script> 
</head> 
<body> 
<div id="results"> When the update occurs, the results will be listed here. </div> 
</body> 
</html>
```

Har inkluderat hela yui-paketet!

Permalänk: https://www.webforum.nu/p/185076

## #2 — Gildebrand, 2010-10-30T09:34Z

Jag har aldrig använt yui, tycker  dock att det såg lite konstigt ut med alla paranteser och sånt runt var callback.

EDIT: Tycker den mesta koden ser konstig ut med alla hakparanteser som är. Har du provat att debugga koden? (Förslagsvis i Google Chrome)

Permalänk: https://www.webforum.nu/p/2258282

## #3 — md7dani, 2010-10-30T10:52Z

> **[Gildebrand skrev:](https://www.webforum.nu/p/1521329)**
>
> Jag har aldrig använt yui, tycker  dock att det såg lite konstigt ut med alla paranteser och sånt runt var callback.
> 
> EDIT: Tycker den mesta koden ser konstig ut med alla hakparanteser som är. Har du provat att debugga koden? (Förslagsvis i Google Chrome)

Inte bunden till skriptet på nåt sätt utan mer ute efter ett ajax-exempel som uppdateras efter x antal sekunder. I exemplet så skriver den inte ut nån tid. :(

Permalänk: https://www.webforum.nu/p/2258288

---

Tråden på webben: https://www.webforum.nu/amne/javascript/185076-exempel-med-ajax
