jQuery ajax返回“对象对象" [英] jQuery ajax returning 'Object Object'

查看:55
本文介绍了jQuery ajax返回“对象对象"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery Ajax将数据发送到PHP脚本.由于某种原因,Ajax请求抛出错误并从PHP脚本-[object Object]

I am trying to send data to a PHP script using jQuery Ajax. For some reason the Ajax request is throwing up an error and returning the following data from the PHP script - [object Object]

我已经在下面复制了我的代码.我还使用页面上其他地方的完全相同的方法复制了代码,看起来似乎很好!

I've copied my code in below. I've also copied code using the exact same method elsewhere on the page which seems to work fine!

谁能解释为什么会这样?

Can anyone explain why this is happening?

首先,代码运行正常

jQuery

 $("#reqtable a").click(function(){
  var cells = []; 
  var name;
  var account;
  var module;
  var email;
  $(this).parent().parent().find("td").each(function(){
  cells.push($(this).html());
  });
  $(this).parent().parent().find("input").each(function(){
  email = $(this).val();
  });
  $(this).parent().parent().prop('id', 'die');
  name = cells[0];
  account = cells[1];
  module = cells [2];

  $.ajax({
            url: "release.php",
            type: "POST",
            data: {name: name, account: account, module: module, email: email},
            success: function(){
            $("#die").remove();
           }
         });
 });

PHP

<?php
include('../../dbconnect.php');

$name = $_POST['name'];
$account = $_POST['account'];
$email = $_POST['email'];
$module = $_POST['module'];

$releasequery = "INSERT INTO release_assignment(name, account, email, module) VALUES ('$name', '$account', '$email', '$module')";
$release = $conn->query($releasequery);

$erasequery = "DELETE FROM request_assignment WHERE email='$email' AND module = $module";
$erase = $conn->query($erasequery);

?>

现在代码不起作用.

jQuery

 $("#downloadtable a").click(function(){
  var dlcells = []; 
  var dlname;
  var dlaccount;
  var dlmodule;
  var dlemail;
  var dlsub;
  var dlpath;

  $(this).parent().parent().find("td").each(function(){
  dlcells.push($(this).html());
  });
  $(this).parent().parent().find("input.dlemail").each(function(){
  dlemail = $(this).val();
  });
  $(this).parent().parent().find("input.dlsub").each(function(){
  dlsub = $(this).val();
  });
  $(this).parent().parent().find("input.dlpath").each(function(){
  dlpath = $(this).val();
  });

  $(this).parent().parent().prop('id', 'die2');
  dlname = dlcells[0];
  dlaccount = dlcells[1];
  dlmodule = dlcells [2];

  $.ajax({
            url: "download.php",
            type: "POST",
            data: {dlname: dlname, dlaccount: dlaccount, dlmodule: dlmodule, dlemail: dlemail, dlsub: dlsub, dlpath: dlpath},
            success: function(data){
            $("#die2").remove();
            },
            error: function(data){
             $('#downloaddiv').html('<p>' + data + '</p>');
            }
         });
 }); 

PHP

<?php
include('../../dbconnect.php');

$name = $_POST['dlname'];
$email = $_POST['dlemail'];
$account = $_POST['dlaccount'];
$module = $_POST['dlmodule'];
$path = $_POST['dlpath'];
$submission = $_POST['dlsub'];

$feedbackquery = "INSERT INTO feedback_assignments(name, email, level, unit, assignmentpath, submission) VALUES ('$name', $email, '$account', '$module', '$path', '$submission')";
$feedback = $conn->query($feedbackquery);

$erasequery = "DELETE FROM uploaded_assignments WHERE email='$email' AND unit = $module";
$erase = $conn->query($erasequery);

?>

当我注释掉所有PHP代码并简单地放入echo ($_POST['dlname']);时,它将返回数据[object Object]

When I comment out all the PHP code and simply put echo ($_POST['dlname']); it returns the data [object Object]

任何人都可以解释发生了什么,为什么它似乎只适用于一个代码块,而不适用于另一个代码块?

Can anyone explain what is going on and why it seems to work with one block of code but not the other?

谢谢!

克里斯

更新:也许值得一提的是,初始链接('#downloadtable a')实际上促使文件下载以及ajax调用,而在有效的代码中,它仅使ajax调用和没有其他的.我不知道这是否在作品中扔了一个扳手,但认为值得一提.

Update: It might be worth mentioning that the initial link ('#downloadtable a') actually instigates a file download as well as the ajax call, whereas in the code that is working it simply makes the ajax call and nothing else. I don't know if this is throwing a spanner in the works but thought it worth mentioning.

更新2:如下所述,使用jQuery Ajax错误回调,我得到以下响应:

Update 2: Using the jQuery Ajax error callback as described below I'm getting the following response:

{"readyState":0,"responseText":"","status":0,"statusText":"error"} 
AJAX error: error : 

我在错误回调中使用的代码如下:

The code I've used in the error callback is as follows:

error: function(jqXHR, textStatus, errorThrown) {
   console.log(JSON.stringify(jqXHR));
   console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
   }

不幸的是,我不明白这是什么意思.谁能对此有所启示?

Unfortunately I don't understand what this means. Can anyone shed any light on this?

更新3 好,我已经找到了Ajax爆炸的原因,并且与更新1(上面)有关.基本上是因为该链接指向文件下载(.docx文件),所以它似乎导致了ajax的问题​​.当我将链接更改为href='#'而不是href="document.docx"时,ajax和PHP脚本有效.

Update 3 OK, I've found the reason for Ajax blowing up on me, and it relates to update number 1 (above). Basically because the link is to a file download (a .docx file) it seems to be causing the problem with ajax. When I change the link to href='#' instead of href="document.docx", the ajax and PHP script works.

这当然引发了一个新问题-如何在更新数据库的同时获取下载文件的链接?

This throws up a new question, of course - how can I get the link to download the file whilst simultaneously updating the database?

推荐答案

指定 dataType 并使用 console 调试数据响应.

Specify a dataType and use console to debug your data response.

还要注意,error回调包含以下参数,而不包含任何数据";

Also, notice that the error callback contains the following arguments and not any "data";

错误类型:函数(jqXHR jqXHR,字符串textStatus,字符串errorThrown)

error Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

更新

目标文件download.php可能引发异常.可能是因为该行中$email周围缺少一些引号;

The target file download.php might be throwing an exception. Possibly because of some missing quotes around $email on the line;

$feedbackquery = "INSERT INTO feedback_assignments(name, email, level, unit, assignmentpath, submission) VALUES ('$name', $email, '$account', '$module', '$path', '$submission')";

调试download.php,并确保它生成了预期的输出/响应.

Debug download.php and make sure it generates the expected output/response.

我建议您转义用于构建SQL查询的值,以防止SQL注入.

I advice you to escape the values you are using to build your SQL query with to prevent SQL injection.

这篇关于jQuery ajax返回“对象对象"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆