[英]Call ASP.net web service method inside succeeded callback function of another method in javascript
好的,這是我的問題:
我有兩個Web服務方法,它們以asmx方式正確調用並返回一個字符串:
namespace myNameSpace
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script,
// using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class myClass : System.Web.Services.WebService
{
[WebMethod]
public string myMethod1()
{
//some code
return ss1; // ss1 is my returned string
}
[WebMethod]
public string myMethod2(string s, string p, string x, string y)
{
//some code
return ss2; // ss2 is my returned string
}
我只想從javascript調用這些方法並獲取正確的字符串值,並在警報窗口中顯示最后返回的值,為此,我在javascript中使用了回調函數:
<script>
function pressButton()
{
myNameSpace.myClass.myMethod1(succeededCallBack1);
function succeededCallBack1(strResponse1)
{
//some operation with strResponse1
myNameSpace.myClass.myMethod2(s, p, x, y, succeededCallBack2);
function succeededCallBack2(strResponse2)
{
alert(strResponse2);
}
}
}
</script>
現在,我已經成功獲取strResponse1並對其進行操作,我還成功地從javascript調用了myMethod2(我在此方法中插入了一個斷點,並且命中了它),並且ss2字符串已正確設置。 問題是在腳本內未調用succededCallBack2函數,並且沒有警報窗口。 問題出在哪兒?
[EDIT1]
我剛剛將方法調用更改為:
<script>
function pressButton()
{
myNameSpace.myClass.myMethod1(succeededCallBack1);
function succeededCallBack1(strResponse1)
{
//some operation with strResponse1
myNameSpace.myClass.myMethod2(s, p, x, y, succeededCallBack2());
function succeededCallBack2(strResponse2)
{
alert(strResponse2);
}
}
}
</script>
現在,我可以看到警報窗口,並且顯然在腳本中調用了succeededCallBack2
函數。 好的,問題是警報窗口顯示了undefined
值。 這意味着未設置strResponse2
。 我在myMethod2
插入一個斷點以查看其是否正常運行並猜測是什么,在命中myMethod2
的斷點之前會顯示警報窗口。 到底怎么可能?
您已經在第一個函數內編寫了第二個函數。 如下糾正。
<script type='text/javascript' language='javascript'>
myNameSpace.myClass.myMethod1(succeededCallBack1);
function succeededCallBack1(strResponse1)
{
//some operation with strResponse
myNameSpace.myClass.myMethod2(s, p, x, y, succeededCallBack2);
}
function succeededCallBack2(strResponse2)
{
alert(strResponse2);
}
</script>
嘗試下面
myNameSpace.myClass.myMethod2(s, p, x, y, function(res) {
alert(res);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.