Jump to content

How to call ajax and return a string?


admin@kdepu.com
 Share

Recommended Posts

Define some function in javascript which will accept a sting (for example in some external js file linked by ServerModule.CustomFiles)

function myfunc(s) {
...
}

When you need some response from server, call uni's ajaxEvent() function from your client js code, on server side in OnAjaxEvent

UniSession.AddJS('myfunc(' + StringToReturn + ');';

Your function will be called with passed string as soon as response reaches client.

Check ajax event demos that come with UniGUI.

Link to comment
Share on other sites

An example:

<html>

<head>
<title>FCKeditor - Sample</title>


<script type="text/javascript">
var xmlHttp,editortxt;


function saveeditor()
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return;
 }
var url="phps/phpinfo1.php";


editortxt=document.getElementById("content21").value;
editortxt="name="+editortxt;
//alert(editortxt);
editortxt=encodeURI(editortxt);


//url=url+"?q="+editortxt;
//url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged; 
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(editortxt);
}
function dodo(){ 
if(xmlHttp.readyState==4){ // xmlHttp下的readystate方法 4表示传送完毕 
if(xmlHttp.status==200){ // xmlHttp的status方法读取状态(服务器HTTP状态码) 200对应OK 404对应Not Found(未找到)等 
document.getElementById("content").innerHTML=xmlHttp.responseText //xmlHttp的responseText方法 得到读取页数据 
} 
} 
} 
function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 


document.getElementById('A4').innerHTML=xmlHttp.responseText;  
//document.getElementById('content21').innerHTML=xmlHttp.responseText; 
 alert(xmlHttp.responseText+'<br>状态:'+xmlHttp.statusText); 
//  alert(xmlHttp.statusText); 
 } 
}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
</script>
</head>
<body>
<form action="javascript:saveeditor();" method="post">
<div> 
       <textarea name="content21"  id="content21" resizable=yes></textarea>
</div> 
<p><b>Response:</b>
<br /><span id="A4"></span>
</p>


<input type="submit" value="提交" />
</form>




</body>
</html>
  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...