【问题标题】:jQuery ajax POST return error in chromejQuery ajax POST在chrome中返回错误
【发布时间】:2013-11-22 13:14:35
【问题描述】:

我使用这个代码

function testfunction(ttl){

        jQuery.ajax({url:"index.php",
           type: "POST",
                   cache : false,
           data: { 'data1': ttl},
           success:function(res){

                  //Do something

         },error: function(requestObject, error, errorThrown) { 
                   console.log(error);

        }});
    }

我得到一个错误 = 'error' 和 errorThrown = ""

此问题仅在谷歌浏览器中。

如何解决这个问题?

【问题讨论】:

  • 在调试工具的网络选项卡上检查请求信息
  • 我的意思是开发者工具
  • 你能发布“错误”吗?
  • 你试过Chrome内置的网络调试器吗?net.tutsplus.com/tutorials/…
  • @Leonardo 你错过了"error = 'error' and errorThrown = """

标签: javascript php jquery ajax google-chrome


【解决方案1】:

以下代码对我有用 - 可能是您通过 ajax 调用发送的参数 ttl 的问题

index.php:

<html>
<head>
    <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
    <script language="JavaScript">
        $('document').ready(function(){
            $('button').click(function() {
                $.ajax({

                    url: "index2.php",
                    type: "POST",
                    cache: false,
                    data: { 'data1': $(this).attr('rel')},
                    success: function (res) {

                        console.log(res);

                    },
                    error: function (requestObject, error, errorThrown) {
                        console.log(error);

                    }
                });
            });
        });
    </script>
</head>
<body>
<button rel="test">test</button>
</body>
</html>

和 index2.php

<?php
echo json_encode($_POST);

【讨论】:

  • 我回显 json_encode($_POST);结果得到 {"data1":"1823"}
  • 那么,如果你使用我的代码,你应该在“res”变量名下获取 json 对象 - 成功部分中的那个。
猜你喜欢
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多