【问题标题】:jQuery Ajax Call not calling successjQuery Ajax 调用未调用成功
【发布时间】:2012-12-18 10:57:20
【问题描述】:

我遇到了一个 ajax 请求问题。每当我运行以下命令时,我都会得到一个x(正如下面请求中的错误函数生成的那样),但我应该得到刻度图像,因为我确认调用确实在系统中进行了更改。

谁能帮我弄清楚我做错了什么?

Ajax 调用:

$.ajax({
    url: "/URL",
    type: "POST",
    dataType: 'json',
    data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
    success: function(){
    $("#user_1_image").attr("src","http://domain.com/check.png");
  },
    error: function(){
    $("#user1_status").html("x");
  }
}); 

将作为输出的数据:

{status: 'OK', payload: ''}

【问题讨论】:

  • 控制台是否出现错误?
  • 我没有使用控制台或任何东西。基本上是一个返回值的 PHP 页面
  • 我认为@ExplosionPills 的意思是您在 Firefox 中遇到任何 javascript 错误吗?
  • 不,Explosion Pills 会询问您是否使用浏览器的控制台(例如 Firefox 中的 Firebug)来跟踪 JavaScript 是否报告了问题。
  • 成功:function(){ 来自 url 的内容是什么?你还没有使用从 url 返回的任何东西

标签: jquery ajax json post


【解决方案1】:

您的 json 字符串中有 Parse 错误,请先检查您的 json 字符串

Parse error on line 1:
{    action: 'assoc_users
-----^
Expecting 'STRING', '}'

我已经更正了您的 json 字符串并进行了尝试。试试这个,它有效,

$(document).ready(function() {
 var actions = { "action": "assoc_users", "username": "testuser", "name": "testname" };
    $.ajax({
    url: "/URL",
    type: "POST",
    data: actions,
    dataType: "json",
    success: function(data){
        alert('action'+data.action+'username'+data.username+'name'+data.name); // this is to check whats coming from the server side
        $("#user_1_image").attr("src","http://domain.com/check.png");
    },
    error: function(jqXHR, exception){
        if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
  }
}); 
});

在php文件中

<?php echo json_encode($_POST); ?>

【讨论】:

    【解决方案2】:

    如果您从指定的 url 获取有效的 json,那么这将起作用,因为在您的代码中,您没有在成功函数中传递数据:

    $.ajax({
        url: "/URL",
        type: "POST",
        dataType:'json',
        data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
        success: function(data){
          if(data){
            $("#user_1_image").attr("src","http://domain.com/check.png");
          }
        },
        error: function(){
           $("#user1_status").html("x");
        }
    });
    

    【讨论】:

    • 我在删除 dataType 时遇到了同样的问题
    • 如果你从那个 url 获取 json,请尝试第二个,然后你必须通过成功函数传递它,如果有数据,然后将 attr 提供给 img。
    【解决方案3】:

    请使用它,可能是引号造成问题,所以试试吧。

    $.ajax({
    url: '/URL',
    type: 'POST',
    dataType: 'json',
    data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
    success: function(){
    $('#user_1_image').attr('src','http://domain.com/check.png');
    },
    error: function(){
    $('#user1_status').html('x');
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-14
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 2015-12-12
      相关资源
      最近更新 更多