AJAX数据返回页面源代码code“或”不确定“不确定、源代码、页面、数据

2023-09-10 21:56:05 作者:不㊣经

我在AJAX的数据有问题。 (我用asp.net web表单),我需要收集页面的DOM元素的数据,我想附加它们的值到表行异步,在同一个页面。只是为了测试我写的警报(数据)的成功的功能。但是,数据恢复源$ C ​​$ C'还是当属未定义。

I am having problem in AJAX data. (I am using asp.net web forms) i need to collect data from page dom elements, as i want to append their values to a table row asynchronously, in the same page. just to test i wrote alert(data) in success function. But data returns 'source code' or comes as 'undefined'.

下面我想2种方式来获得在阿贾克斯HTML和JSON数据。结果是相同的。

Below i tried 2 ways to get data in ajax both HTML and JSON. results are the same.

Ajax和JSON数据类型:

ajax with JSON dataType:

$(document).ready(function () {
    $('img').live('click', function (event) {
        event.preventDefault();

        var number= $(this).attr('id');
        var date= '06.04.2015';
        var name= "xyz";

        $.ajax({
            type: 'post',
            url: 'treatment.aspx',
            contentType: "application/json;charset=utf-8",
            dataType:JSON,
            data: JSON.stringify({
                number: number,
                date: date,
                name: name
            }),
            success: function (data) {
                alert(data);
            } 
        });
    }); 
});

Ajax和HTML数据类型:

ajax with html dataType:

$.ajax({
            type: 'post',
            cache: false,
            url:'treatment.aspx',
            contentType:"text/html",
            dataType: 'html',
            data:"some data",

            success: function (data) {
                document.getElementById("demo").innerHTML = data;
            }

        });

(我也试过警报(数据[0] .number)和警报(data.number),没有工作。)

(I also tried alert(data[0].number) and alert(data.number), not working.)

感谢。

推荐答案

试试这个codeI认为它可能工作..

Try with this code i think it might work..

$(document).ready(function () {
$('img').live('click', function (event) {
    event.preventDefault();

var jsonObject = new Object();
jsonObject.number = $(this).attr('id');
jsonObject.date   = 'date value';
jsonObject.name   = 'desired name';
var jsonData      = json.parse(jsonObject);

           $.ajax({
url: "your orders",
type: "POST",
data: jsonData,
dataType: "json"
});      
        success: function (data) {
            alert(data);
        } 
    });
}); 
});