javascript - Ajax如何處理這樣的資料?如何呼叫後台方法? (response資料為什麼不顯示return的字串?)
淡淡烟草味
淡淡烟草味 2017-05-19 10:17:24
0
3
560

1、asp.net項目,前台使用ajax向後台傳值,怎麼能確定有沒有呼叫到後台的方法呢?設斷點根本走,然後回傳結果也看不出什麼問題就是直接回應error方法了。求指教!

頁:

# 後台:

#瀏覽器:

#response:

##
淡淡烟草味
淡淡烟草味

全部回覆(3)
Ty80

你發送ajax請求時,指定要接收的資料類型為 json 而你后端返回的数据不是json個格式。所以認定為請求失敗,進入失敗回呼。

而且你後端寫的有問題吧。瀏覽器的response是一個HTML頁面?

這些地方應該是response.write('xxxx')

然後response.end()

後端回應資料不都應該是response.write嗎?


補充:

肯定寫錯了吧

html

    <pre id="test">

    </pre>
    <pre id="test2">

    </pre>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
        $.ajax({
            url: "./test.aspx",
            dataType: 'json'
        }).done(function (data) {
            // 指定为类型为json 则会将数据处理为对象的 即自动JSON.parse 了
            
            $('#test').text('data类型' + typeof data + '\n' + JSON.stringify(data, null, 4));
        }).fail(function () {
            alert('fail');
        });

        $.ajax({
            url: "./test.aspx"
        }).done(function (data) {
            alert('data类型' + typeof data + '\n');
            $('#test2').text('data类型' + typeof data + '\n' + data);
        }).fail(function () {
            alert('fail');
        });
    </script>

test.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{   
    String str = @"[{
    ""guid"": ""410000197812057474"",
    ""name"": ""当进少头等程给进话团拉组层育除水"",
    ""url"": ""mailto://cixus.vg/wvgermuy"",
    ""date"": ""03-25"",
    ""status"": """",
    ""openType"": ""tabsnav""
}, {
    ""guid"": ""42000020111028506X"",
    ""name"": ""张度联酸做电往"",
    ""url"": ""mailto://pslic.sa/mhsgmxqur"",
    ""date"": ""08-08"",
    ""status"": """",
    ""openType"": ""dialog""
}, {
    ""guid"": ""520000199308053591"",
    ""name"": ""政劳则加派党王存才从她"",
    ""url"": ""telnet://lvgoxn.iq/imsfzmfjk"",
    ""date"": ""06-06"",
    ""status"": """",
    ""openType"": ""dialog""
}]";
    Response.Write(str);
    Response.End();
}

小葫芦

ajax去掉datatype:json
或Response.Write返json格式資料

大家讲道理

後台 return 的時候改成:

return {
    success: true
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!