这里我简单的样本代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace PopUpFromCsFile
{
public class PopUpWindow
{
public void PopUpFromCsFile()
{
string str1 = "<script> $.ajax({" +
"type: GET ," +
"url: Service/Class1.cs/callfromjs ," +
"data: {} ," + "success: function () { getDetails(); } " +
"});</script>";
page.ClientScript.RegisterStartupScript(this.GetType(), "script1", str1);
}
[WebMetod]
public string CallFromJs()
{
return "santosh";
}
public void getDetails()
{
string str = "<script>alert( Hai );</script>";
System.Web.UI.Page page = (System.Web.UI.Page)HttpContext.Current.Handler;
page.ClientScript.RegisterStartupScript(this.GetType(), "script2", str);
}
}
}
Explanation
在 PopUpFromCsFile () 中, 我为 Ajax+WebMethod 写入了代码。 Url 路径是正确的吗, 我无法从脚本中调用 Retails () 。