微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Jquery Uncaught TypeError:Object#没有方法’split’

当我尝试运行此页面时,通过单击下拉列表,更改事件正在发送AJAX请求,但它显示错误:“未捕获的TypeError:对象#没有方法’拆分’”.为什么显示拆分不是方法

 <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ajax - jQuery AJAX Powered Cascaded drop down list</title>
        <script type="text/javascript" src="JS/jquery-1.7.2.js"></script>

        <script type="text/javascript">

        //When the socument is ready call the function OnReady
        $(document).ready(OnReady);

        function OnReady() 
        {  
            //Handle the change event for the drop down list
            $("#drpContinent").change(onChange);        
        }    

        function onChange()
        { 
            //create the ajax request
            $.ajax
            ( 
                {
                    type: "POST", //HTTP method
                    url: "Default2.aspx/OnContinentChange", //page/method name
                    data: "{'continentName':'"+$('#drpContinent').val() +"'}", //json to represent argument
                    contentType: "application/json; charset=utf-8", 
                    dataType: "json",
                    success: callback,
                    error: one rror
                }
            );

        }

        //Handle the callback on success
        function callback(msg)
        { 
            //handle the callback to handle response                
            //request was successful. so Retrieve the values in the response.
            var countries = msg.split(';');
            var length = countries.length;

            //Change the second dropdownlists items as per the new values foudn in response.
            //let us remove existing items
            document.getElementById('<%=drpCountry.ClientID %>').options.length = 0;

            //Now add the new items to the dropdown.
            var dropDown = document.getElementById('<%=drpCountry.ClientID %>');
            for(var i = 0; i < length - 1; ++i) 
            {
                var option = document.createElement("option");
                option.text = countries[i];
                option.value = countries[i];

                dropDown.options.add(option);
            }
        }

        //Handle the callback on error
        function one rror()
        {
            alert('something went wrong');
        }

        </script>

    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
                Select Continent:
                <asp:DropDownList ID="drpContinent" runat="server">
                </asp:DropDownList><br />
                <br />
                Select Country: &nbsp;
                <asp:DropDownList ID="drpCountry" runat="server">
                </asp:DropDownList>
            </div>
        </form>
    </body>
    </html>

这里我们有OnContinentChange方法来处理AJAX请求:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            //Let us populate the list of continents in the first drop down
            drpContinent.DataSource = DBHelper.GetContinentList();
            drpContinent.DataTextField = "continentName";
            drpContinent.DataValueField = "continentName";
            drpContinent.DataBind();

            //Set the second dropdown as the list of all countries of selected continent
            drpCountry.DataSource = DBHelper.GetCountriesList(drpContinent.SelectedValue);
            drpCountry.DataTextField = "countryName";
            drpCountry.DataValueField = "countryName";
            drpCountry.DataBind();
        }
    }

    [System.Web.Services.WebMethod]
    public static string OnContinentChange(string continentName)
    {
        DataTable table = DBHelper.GetCountriesList(continentName.Trim());

        string result = string.Empty;

        foreach (DaTarow r in table.Rows)
        {
            result += r["countryName"].ToString() + ";";
        }

        return result;
    }
}

解决方法:

您说dataType是JSON,这意味着响应将是一个对象而不是一个字符串,您只能对字符串使用split().

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐