搜尋

首頁  >  問答  >  主體

javascript - Ajax如何處理這樣的資料?如何呼叫後台方法? (response資料為什麼不顯示return的字串?)

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

頁:

# 後台:

#瀏覽器:

#response:

##
淡淡烟草味淡淡烟草味2852 天前638

全部回覆(3)我來回復

  • PHP中文网

    PHP中文网2017-05-19 10:19:24

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

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

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

    然後response.end()

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


    補充:

    肯定寫錯了吧

    html

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    <code class="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></code>

    test.aspx.cs

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    <code class="c#">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();

    }</code>

    回覆
    0
  • 高洛峰

    高洛峰2017-05-19 10:19:24

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

    回覆
    0
  • 大家讲道理

    大家讲道理2017-05-19 10:19:24

    後台 return 的時候改成:

    1

    2

    3

    <code>return {

        success: true

    }</code>

    回覆
    0
  • 取消回覆