Ajax代码无法正常工作。 [英] Ajax Code not working.

查看:90
本文介绍了Ajax代码无法正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在c#中有asp.net网站。



我'使用Ajax从数据库中检索数据。



代码:



< ; script type =text / javascript> 
函数GetDescription(a){
alert(a); //下拉项目选择值
$ .ajax({
类型:'POST',
contentType:application / json; charset-8;,
url:' Default.aspx / GetRef',
dataType:'json',
data:{'id':'+ a +'},
success:function(result){
alert(result);
}
});

}
< / script>





Dropdownlist onchange()

< asp:DropDownList ID =ddlschrunat =serverWidth =80pxonchange =GetDescription(this.value)> 
< / asp:DropDownList>





Default.aspx / GetRef

 [WebMethod] 
public string GetRef(string id)
{
DataTable dt = new DataTable();
SqlParameter [] p = new SqlParameter [1];
p [0] = new SqlParameter(@ RefID,Convert.ToInt32(id));
dt = dl.GetDataWithParameters(Sp_WT_GetRef,p);

string data = dt.Rows [0] [Description]。ToString()+|+ dt.Rows [0] [PriceInUSD]。ToString();

返回数据;
}





错误

 http:// localhost:54576 /AutomobileWebApp/Default.aspx/GetRef无法加载资源:服务器响应状态为500(内部服务器错误)
http:// localhost:54576 / resources / demos / style.css无法加载资源:服务器回复状态为404(未找到)





任何人都可以帮助我,我错了...



谢谢

解决方案

.ajax({
type:'POST',
contentType:application / json; charset-8;,
url:'Default.aspx / GetRef',
dataType:'json',
data:{'id':' + a +'},
成功:函数(结果){
alert(result);
}
});

}
< / script>





Dropdownlist onchange()

< asp:DropDownList ID =ddlschrunat =serverWidth =80pxonchange =GetDescription(this.value)> 
< / asp:DropDownList>





Default.aspx / GetRef

 [WebMethod] 
public string GetRef(string id)
{
DataTable dt = new DataTable();
SqlParameter [] p = new SqlParameter [1];
p [0] = new SqlParameter(@ RefID,Convert.ToInt32(id));
dt = dl.GetDataWithParameters(Sp_WT_GetRef,p);

string data = dt.Rows [0] [Description]。ToString()+|+ dt.Rows [0] [PriceInUSD]。ToString();

返回数据;
}





错误

 http:// localhost:54576 /AutomobileWebApp/Default.aspx/GetRef无法加载资源:服务器响应状态为500(内部服务器错误)
http:// localhost:54576 / resources / demos / style.css无法加载资源:服务器回复状态为404(未找到)





任何人都可以帮助我,我错了...



谢谢


确保webmethod是静态的,并使用.d来获得结果。如果你在第一个警告框中得到未定义,那么问题是你的js没有正确读取下拉列表的值。



 <   asp:DropDownList     ID   =  ddlsch < span class =code-attribute>   runat   =  server   宽度  =   80px    onchange   =  GetDescription(this.value)   >  
< asp:ListItem 文字 = 一个 = 1 / >
< asp:ListItem 文字 = 两个 = 2 / &g t;
< asp:ListItem 文本 = = < span class =code-keyword> 3 / >
< / asp:DropDownList >

< script type = text / javascript >
函数GetDescription(a){
警报(a); //下拉项目选择值

.ajax({
类型:'POST',
contentType:application / json; charset-8;,
url:'Default.aspx / GetRef',
dataType:'json',
data:{'id':'+ a +'},
成功:function(result){
alert(result.d);
}
});

}
< / script >





 [WebMethod] 
< span class =code-keyword> public static string GetRef( string id)
{
// 您的实际这里的代码
return id;
}


Hi guys,

I have asp.net web site in c#.

I'm using Ajax to retrieve data from database.

Code:

<script type="text/javascript">
        function GetDescription(a) {
            alert(a); // the dropdown item selected value
                $.ajax({
                    type: 'POST',
                    contentType: "application/json; charset-8;",
                    url: 'Default.aspx/GetRef',
                    dataType: 'json',
                    data: "{ 'id':'" + a + "'}",
                    success: function (result) {
                        alert(result);
                    }
                });

        }
    </script>



Dropdownlist onchange()

<asp:DropDownList ID="ddlsch" runat="server" Width="80px"                                                         onchange="GetDescription(this.value)" >
</asp:DropDownList>



Default.aspx/GetRef

[WebMethod]
    public string GetRef(string id)
    {
        DataTable dt = new DataTable();
        SqlParameter[] p = new SqlParameter[1];
        p[0] = new SqlParameter("@RefID", Convert.ToInt32(id));
        dt = dl.GetDataWithParameters("Sp_WT_GetRef", p);

        string data = dt.Rows[0]["Description"].ToString() +"|"+ dt.Rows[0]["PriceInUSD"].ToString();

        return data;
    }



Error

http://localhost:54576/AutomobileWebApp/Default.aspx/GetRef Failed to load resource: the server responded with a status of 500 (Internal Server Error)
http://localhost:54576/resources/demos/style.css Failed to load resource: the server responded with a status of 404 (Not Found)



Can anyone please help me where i'm wrong...

Thanks

解决方案

.ajax({ type: 'POST', contentType: "application/json; charset-8;", url: 'Default.aspx/GetRef', dataType: 'json', data: "{ 'id':'" + a + "'}", success: function (result) { alert(result); } }); } </script>



Dropdownlist onchange()

<asp:DropDownList ID="ddlsch" runat="server" Width="80px"                                                         onchange="GetDescription(this.value)" >
</asp:DropDownList>



Default.aspx/GetRef

[WebMethod]
    public string GetRef(string id)
    {
        DataTable dt = new DataTable();
        SqlParameter[] p = new SqlParameter[1];
        p[0] = new SqlParameter("@RefID", Convert.ToInt32(id));
        dt = dl.GetDataWithParameters("Sp_WT_GetRef", p);

        string data = dt.Rows[0]["Description"].ToString() +"|"+ dt.Rows[0]["PriceInUSD"].ToString();

        return data;
    }



Error

http://localhost:54576/AutomobileWebApp/Default.aspx/GetRef Failed to load resource: the server responded with a status of 500 (Internal Server Error)
http://localhost:54576/resources/demos/style.css Failed to load resource: the server responded with a status of 404 (Not Found)



Can anyone please help me where i'm wrong...

Thanks


Make sure the webmethod is static, and use the .d to get the result. If you're getting "undefined" in the first alert box then the problem is that your js isn't reading the value of the dropdown correctly.

<asp:DropDownList ID="ddlsch" runat="server" Width="80px" onchange="GetDescription(this.value)" >
    <asp:ListItem Text="One" Value="1" />
    <asp:ListItem Text="Two" Value="2" />
    <asp:ListItem Text="Three" Value="3" />
</asp:DropDownList>

<script type="text/javascript">
    function GetDescription(a) {
        alert(a); // the dropdown item selected value


.ajax({ type: 'POST', contentType: "application/json; charset-8;", url: 'Default.aspx/GetRef', dataType: 'json', data: "{ 'id':'" + a + "'}", success: function (result) { alert(result.d); } }); } </script>



[WebMethod]
public static string GetRef(string id)
{
    // your actual code here
    return id;
}


这篇关于Ajax代码无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆