recherche

Maison  >  Questions et réponses  >  le corps du texte

javascript - 利用jQuery发送ajax异步请求

直接上代码

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

29

30

31

<code class="lang-javascript"><br>    $('form').submit(function (e) {

        // 阻止表单默认提交

        e.preventDefault();

 

        var name = $('#name').val();

        var password = $('#password').val();

 

        var data = JSON.stringify({"name": name, "password": password});

 

        // 异步发送数据

        $.ajax({

            // 数据传送方式

            type: 'POST',

            contentType: "application/json; charset=utf-8",

            // 数据处理文件

            url: 'process.php',

            // JSON格式数据

            data: data,

            dataType: 'json',

            // @msg: 数据返回值

            success: function (msg, status) {

                console.log(msg);

            },

            // Degbug

            error: function (xhr, desc, err) {

                console.log(xhr);

                console.log("Details: " + desc + "\nError:" + err);

            }

        });

    });

</code>

数据传输一直是error
报错如下

1

2

3

<code>Details: parsererror

Error:SyntaxError: Unexpected token N

</code>

阿神阿神2886 Il y a quelques jours695

répondre à tous(3)je répondrai

  • 黄舟

    黄舟2017-04-10 13:12:31

    这个报错应该是你后台返回的数据的格式不是标准json格式,解析错误。

    调试网络请求返回json格式的最简单方法,用chrome 调试工具找到这个请求,看返回的值。

    如果preview里是纯文本,就说明格式错误。

    如果preview里是如图的对象,就说明格式正确。

    另,多多关注我的小站:http://www.html-js.com 专注前端

    répondre
    0
  • ringa_lee

    ringa_lee2017-04-10 13:12:31

    拿到返回到的json.检测一下格式对不对.

    这个可以帮你

    http://www.bejson.com/go.php?u=http://www.bejson.com/index.php

    répondre
    0
  • 大家讲道理

    大家讲道理2017-04-10 13:12:31

    应该就是返回的数据格式有误,不是json。@代码诗人芋头 说了,如果不是的话,他自切丁丁。

    répondre
    0
  • Annulerrépondre