How to connect ESP8266-01 (AT-commands) to its web server using AJAX I am just new on using AJAX in web server, I got an error on declaring "server" I am confused about this because I just used only a AT commands to used the ESP8266-01 (wifi-module) I hope you can help me, I want to get the data (distance) to print in the web server(page) in real-time(no refresh). So I used the AJAX for it but this code is still error. Advance thanks on anyhelp :)

 #include "NewPing.h"
 #include &lt;SoftwareSerial.h&gt;
 #define DEBUG true

 SoftwareSerial esp8266(10,11);
 #define TRIGGER_PIN 4
 #define ECHO_PIN 3
 #define MAX_DISTANCE 400

 NewPing sonar(TRIGGER_PIN,ECHO_PIN,MAX_DISTANCE);

 void setup() {
 Serial.begin (9600);
 Serial.println("START");
 esp8266.begin(9600);

 server.on("/",WebsiteContent);
 server.on("/xml",XMLcontent);
 server.begin();

 pinMode(TRIGGER_PIN, OUTPUT);
 pinMode(ECHO_PIN, INPUT);

 sendData("AT+RST\r\n",2000,DEBUG);
 sendData("AT+CWMODE=2\r\n",1000,DEBUG);
 sendData("AT+CIFSR\r\n",1000,DEBUG);
 sendData("AT+CIPMUX=1\r\n",1000,DEBUG);       
 sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG);


}

void javascriptContent(){
Javascript ="&lt;SCRIPT&gt;\n";
Javascript+="var xmlHttp=createXmlHttpObject();\n";
Javascript+="function createXmlHttpObject(){\n";
Javascript+="if(window.XMLHttpRequest){\n";
Javascript+="xmlHttp=new XMLHttpRequest();\n";
Javascript+="}else{\n";
Javascript+="xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');\n";
Javascript+="}\n";
Javascript+="return xmlHttp;\n";
Javascript+="}\n";
Javascript+="\n";
Javascript+="function response(){\n";
Javascript+="xmlResponse=xmlHttp.responseXML;\n";
Javascript+="xmldoc = xmlResponse.getElementsByTagName('data');\n";
Javascript+="message = xmldoc[0].firstChild.nodeValue;\n";
Javascript+="document.getElementById('div1').innerHTML=message;\n";
Javascript+="}\n";

Javascript+="function process(){\n";
Javascript+="xmlHttp.open('PUT','xml',true);\n";
Javascript+="xmlHttp.onreadystatechange=response;\n";
Javascript+="xmlHttp.send(null);\n";
Javascript+="setTimeout('process()',200);\n";
Javascript+="}\n";

Javascript+="&lt;/SCRIPT&gt;\n";


}
void WebsiteContent(){
javascriptContent();

 Website="&lt;html&gt;\n";
 Website+="&lt;style&gt;\n";
 Website+="#div1{\n";
 Website+="width:400px;\n";
 Website+="margin:0 auto;\n";
 Website+="margin-top:130px;\n";
 Website+="font-size:900%;\n";
 Website+="color:powderblue;\n";
 Website+="}\n";
 Website+="&lt;/style&gt;\n";
 Website+="&lt;body onload='process()'&gt;";
 Website+="&lt;div id='div1'&gt;"+data+"&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;";
 Website+=Javascript;
 server.send(200,"text/html",Website);
 }
 void XMLcontent(){

 XML ="&lt;?xml version='1.0'?&gt;";
 XML+="&lt;data&gt;";
 XML+=data;
 XML+="&lt;/data&gt;";

 server.send(200,"text/xml",XML);

 }
 void loop()
 {

 delay(2000);  
 digitalWrite(TRIGGER_PIN, LOW);
 delayMicroseconds(2);
 digitalWrite(TRIGGER_PIN, HIGH);
 delayMicroseconds(10);
 digitalWrite(TRIGGER_PIN, LOW);

 long duration,distance;
 duration= pulseIn(ECHO_PIN,HIGH);

 distance = duration/58.2;

 data=(String)distance;
 delay(500); 

 server.handleClient();


 if(esp8266.available()) 
 {    
 if(esp8266.find("+IPD,"))
 {
 delay(1000);
 int connectionId = esp8266.read()-48;


 String cipSend = "AT+CIPSEND=";
 cipSend += connectionId;
 cipSend += ",";
 cipSend +=webpage.length();
 cipSend +="\r\n";
 sendData(cipSend,1000,DEBUG);
 sendData(webpage,1000,DEBUG);    
 String closeCommand = "AT+CIPCLOSE="; 
 closeCommand+=connectionId; 
 closeCommand+="\r\n";
 sendData(closeCommand,3000,DEBUG);

}
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";   
esp8266.print(command); 
long int time = millis();
while( (time+timeout) &gt; millis())
{
  while(esp8266.available())
  {
    char c = esp8266.read(); 
    response+=c;
  }  
}
if(debug)
{
  Serial.print(command);
  Serial.print("&gt; ");
  Serial.print(response);
}
return response;
}
You're trying to mix code that is intended to run directly on an ESP8266 using ESP8266-specific libraries on an Arduino.  It can't ever work.

Likes like:

server.on("/",WebsiteContent);
server.on("/xml",XMLcontent);
server.begin();


are for use with the ESP8266's ESP8266WebServer library which doesn't run on an Arduino. To use these kind of constructs you have to program the ESP8266, not the Arduino, and then you don't have the AT commands available. 

So, either:


Do it all with AT commands only, or
Ditch the Arduino and program the ESP8266 directly instead.