如何读取JQuery webservice返回的XML数据

时间:2022-10-24 21:33:56

ASMX:

ASMX:

    public class ItemRecord
    {
        public string model { get;set; }
        public string verzia { get;set; }
        public string typ { get;set; }
        public string motor { get;set; }
    }

    [WebMethod]
    public ItemRecord GetRecord(int id)
    {
        ItemRecord record = new ItemRecord();

        using (SqlConnection sConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["bazarovkyConnectionString"].ToString()))
        {
            sConnection.Open();


            SqlCommand sCommand = sConnection.CreateCommand();
            sCommand.CommandText = "select id_model, motor from data_motorizacia where id=@id";
            sCommand.Parameters.AddWithValue("@id", id);

            SqlDataReader sReader = sCommand.ExecuteReader();
            if (sReader.Read())
            {
                record.model = sReader["id_model"].ToString();
                record.motor = sReader["motor"].ToString();
            }
        }

        return record;

    }

JQUERY:

JQUERY:

id = $("#view_id", row).text();

$.ajax({
    type: "POST",
    url: "/data.asmx/GetRecord",
    data: "{'id':'" + id + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "xml",
    processData: false,
    error: function (XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); },
    success: function (xml) { ajaxFinish(xml); }
});

function ajaxFinish(xml) {
    // parse the object
    alert($(xml).find("motor").text());

}

What I want to do is to read value of motor and id_model in ajaxFinish. But it either returns object (if I refer to XML only) or unspecified or empty.

我要做的是读取ajaxFinish中的motor和id_model的值。但它要么返回对象(如果我只引用XML),要么不指定或为空。

How do I track/debug what was returned from .ASMX file?

如何跟踪/调试.ASMX文件返回的内容?

2 个解决方案

#1


1  

You can try to use JSON instead of xml

您可以尝试使用JSON而不是xml

#2


2  

i have not copy pasted your code, so its bit different but you'll get the idea...

我没有复制粘贴你的代码,所以它有点不同,但是你会明白的。

  var arr= new Array();
  var loopCounter = 0;

$.ajax({
            type: "POST",
            url: "yourURL",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('xmlNode').each(function() {
                    arr[loopCounter] = $(this).find('xmlNode').text();
                    loopCounter += 1;
                });

here arr is the array in which the values are read and loopCounter is used to store the data in the array arr, xmlNode is the name of the node in your xml (which you get in response) wwhose value you want to get

在这里,arr是读取值的数组,loopCounter用于在数组arr中存储数据,xmlNode是您的xml(您在响应中得到的)中希望获取的节点的名称

#1


1  

You can try to use JSON instead of xml

您可以尝试使用JSON而不是xml

#2


2  

i have not copy pasted your code, so its bit different but you'll get the idea...

我没有复制粘贴你的代码,所以它有点不同,但是你会明白的。

  var arr= new Array();
  var loopCounter = 0;

$.ajax({
            type: "POST",
            url: "yourURL",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('xmlNode').each(function() {
                    arr[loopCounter] = $(this).find('xmlNode').text();
                    loopCounter += 1;
                });

here arr is the array in which the values are read and loopCounter is used to store the data in the array arr, xmlNode is the name of the node in your xml (which you get in response) wwhose value you want to get

在这里,arr是读取值的数组,loopCounter用于在数组arr中存储数据,xmlNode是您的xml(您在响应中得到的)中希望获取的节点的名称