PDA

View Full Version : Làm sao để có thể sử dụng giá trị lấy từ Webmethod trong Ajax?



nmd
25-02-2008, 18:23
Mình đang làm 1 ví dụ đơn giản, sử dụng Ajax. Mình lấy giá trị lấy từ WebMethod của Web Service và sử dụng cho việc khác. Vấn đề là cái biến đó (var text) khi ở ngoài thì chỉ còn là null thôi. Khi alert(text+" dsfdfsa"); thì nó trả về null thôi.

Xin vui lòng chỉ mình cách khắc phục với. Xin cám ơn nhiều.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xh.../xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
<Services>
<asp:ServiceReference Path="~/WebService3.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager>
<div>
<input id="Location" type="text" />
<input id="GetNumLocations" type="button" value="Get Count" onclick="getLocations()" /> (examples: 92618, 90623, 90017)
<div id="NumLocations"></div>
</div>
</div>
</form>

<script type="text/javascript" language="javascript">
<!--
var text="";
getLocations1();

function getLocations1(){
//var zip = $get("Location").value;
var zip = "92618";
DUC.WebService3.GetLocationCount(zip, onGetLocationSuccess, onGetLocationFailure, "<%= DateTime.Now %>");
}
function onGetLocationSuccess(result, context, methodName){
$get("NumLocations").innerHTML = result + " location(s) found."+"context:"+context+" methodName"+methodName;
text=result;
alert(text);
}

function onGetLocationFailure(error, context, methodName){
var errorMessage = error.get_message();
$get("NumLocations").innerHTML = errorMessage;
}

alert(text+" dsfdfsa");
//-->
</script>
</body>
</html>