jquery ajax 调用给出错误,与 RESTful API 结合使用

发布于 2024-12-23 14:30:01 字数 1955 浏览 2 评论 0原文

我需要一些帮助来分析为什么下面的简短 jquery 代码不起作用。 ajax 调用基于 RESTful API,我已经确认这个确切的调用可以工作。所以不存在后端问题。我只关心这里的前端,我缺乏经验。我认为以下内容应该相当简单,我看不出是什么导致ajax出错。难道有什么明显的错误吗?

<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#submit").click(function () {
                var data = {};
                data["login_name"] = $("#login_name")[0].value;
                data["password"] = $("#password")[0].value;

                $.ajax({
                    type: "POST",
                    url: "https://website.com/a/login",
                    data: data,
                    dataType: "json"
                }).success(function (data) {
                    $("#output")[0].value = JSON.stringify(data, null, "\t");
                }).error(function (a, b, c) {
                    $("#output")[0].value = alert(JSON.stringify(c, null, "\t"));
                });
                $("#output")[0].value = "Sending...";
            });
        });
    </script>
</head>
<body>
    <span class="value">
        <input type="text" class="value" id="login_name" style="width:350px" />
    </span>
    <span class="value">
        <input type="text" class="value" id="password" style="width:350px" />
    </span>
    <span>
        <input type="button" id="submit" value="Send Request" />
    </span>
    <textarea id="output" readonly="readonly"></textarea>
</body>

我只能怀疑数据有问题。我通过修改一些现有代码创建了上面的代码:

var data = {};

    $("#fields div.entry").each(function(i,elem) {

        elem = $(elem);

        data[elem.find("input.field")[0].value]=elem.find("input.value")[0].value;

    });

上面的代码是在创建数据的原始工作代码中,我看不出它与我创建的数据数组有什么不同......

I need some help analyzing why the following short jquery code is not working. The ajax call is based off RESTful API and I've confirmed this exact call to work. So there's no backend issues. I'm just concerned with the frontend here, which I lack experience in. I think the following should be fairly straightforward, and I can't see what it is that makes the ajax go to error. Is there something blatantly wrong?

<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#submit").click(function () {
                var data = {};
                data["login_name"] = $("#login_name")[0].value;
                data["password"] = $("#password")[0].value;

                $.ajax({
                    type: "POST",
                    url: "https://website.com/a/login",
                    data: data,
                    dataType: "json"
                }).success(function (data) {
                    $("#output")[0].value = JSON.stringify(data, null, "\t");
                }).error(function (a, b, c) {
                    $("#output")[0].value = alert(JSON.stringify(c, null, "\t"));
                });
                $("#output")[0].value = "Sending...";
            });
        });
    </script>
</head>
<body>
    <span class="value">
        <input type="text" class="value" id="login_name" style="width:350px" />
    </span>
    <span class="value">
        <input type="text" class="value" id="password" style="width:350px" />
    </span>
    <span>
        <input type="button" id="submit" value="Send Request" />
    </span>
    <textarea id="output" readonly="readonly"></textarea>
</body>

I can only suspect that something is wrong with data. I created the above code by modifying some existing code:

var data = {};

    $("#fields div.entry").each(function(i,elem) {

        elem = $(elem);

        data[elem.find("input.field")[0].value]=elem.find("input.value")[0].value;

    });

the code above was in the original working code which created data, I cannot see how it is different from the data array I created....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2024-12-30 14:30:01

作为一个快速猜测,您可以尝试将内容类型添加到帖子中:

        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: 'https://website.com/a/login',
            data: data,
            dataType: 'json'

As a quick guess you can try to add the content-type to the post:

        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: 'https://website.com/a/login',
            data: data,
            dataType: 'json'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文