PHP AJAX 响应的 `response.status` 返回未定义。

huangapple go评论116阅读模式
英文:

PHP AJAX response.status return undefined

问题

Here is the translation of the non-code part of your text:

我确实有两个问题,第一个是当我尝试评估 response.status 时,它显示为 undefined;第二个是我的 "$_SESSION['promo-code'] = $arr" 也没有被创建。当输入促销码时,我得到以下内容:

AJAX

  1. function applyPromo(){
  2. var promo = $("#promo-code-value").val().trim();
  3. console.log(promo);
  4. $.ajax({
  5. url: "get-promo.php",
  6. type: "POST",
  7. data: { getpromo: promo },
  8. success: function (response) {
  9. console.log("%j", response);
  10. console.log(response.status);
  11. if (response.status === "success") {
  12. swal("Good job!!",
  13. "Promo code applied Successfully!",
  14. "success"
  15. ).then(() => {
  16. reloadPage();
  17. });
  18. } else {
  19. swal("Humm...", "Promo code no longer valid", "error");
  20. }
  21. }
  22. })
  23. }

PHP

  1. if(isset($arr)){
  2. //将数组添加到会话变量中
  3. //**每次只能应用一个代码。会覆盖之前的
  4. $_SESSION['promo-code'] = $arr;
  5. //成功响应
  6. echo json_encode(array(
  7. 'status' => 'success',
  8. 'message' => $arr, //仅用于调试,将被替换为成功消息
  9. ));
  10. } else {
  11. echo json_encode(array(
  12. 'status' => 'error',
  13. 'message' => 'error message'
  14. ));
  15. }

这只是为了学校项目。谢谢!

我尝试将 "success" 切换为 "error",但它似乎根本不进行评估。我对 Ajax 或 PHP 不熟悉,但我相信我曾经看到其他人是这样做的。

英文:

I have two problems really here, the first one being that when I try to evaluate response.status I get undefined and the second one being that my "$_SESSION['promo-code'] = $arr" is also not being created. When entering a promo code, I get the following :
PHP AJAX 响应的 `response.status` 返回未定义。

AJAX

  1. function applyPromo(){
  2. var promo = $("#promo-code-value").val().trim();
  3. console.log(promo);
  4. $.ajax({
  5. url:"get-promo.php",
  6. type: "POST",
  7. data:{ getpromo : promo},
  8. success: function(response){
  9. console.log("%j", response);
  10. console.log(response.status);
  11. if(response.status === "success"){
  12. swal("Good job!!",
  13. "Promo code applied Successfully!",
  14. "success"
  15. ).then(() => {
  16. reloadPage();
  17. });
  18. }else{
  19. swal("Humm...", "Promo code no longervalid","error");
  20. }
  21. }
  22. })
  23. }

PHP

  1. if(isset($arr)){
  2. //Add array to session variable
  3. //** Only one code can be apply at the time . will override
  4. $_SESSION['promo-code'] = $arr;
  5. //Success response
  6. echo json_encode(array(
  7. 'status' => 'success',
  8. 'message'=> $arr, //For debugging only , will be replaced to success message
  9. ));
  10. }else {
  11. echo json_encode(array(
  12. 'status' => 'error',
  13. 'message'=> 'error message'
  14. ));
  15. }

It's only for a school project. Thanks!!

Ive tried switching "success" to "error" but it is simply not evaluating .I'm new to ajax or php but I believe to have seen others makes it work that way.

答案1

得分: 0

问题之一是响应是JSON格式的。因此,您必须将其转换为JS对象,然后您就可以获取状态。以下是更新后的代码:

  1. function applyPromo(){
  2. var promo = $("#promo-code-value").val().trim();
  3. console.log(promo);
  4. $.ajax({
  5. url:"get-promo.php",
  6. type: "POST",
  7. data:{ getpromo : promo},
  8. success: function(response){
  9. let response = JSON.parse(response);
  10. console.log("%j", response);
  11. console.log(response.status);
  12. if(response.status === "success"){
  13. swal("Good job!!",
  14. "Promo code applied Successfully!",
  15. "success"
  16. ).then(() => {
  17. reloadPage();
  18. });
  19. }else{
  20. swal("Humm...", "Promo code no longer valid","error");
  21. }
  22. }
  23. })
  24. }

问题之二是,您没有使用session_start()。以下是更新后的代码:

  1. if(isset($arr)){
  2. //将数组添加到会话变量
  3. //** 一次只能应用一个代码,将覆盖
  4. session_start();
  5. $_SESSION['promo-code'] = $arr;
  6. //成功响应
  7. echo json_encode(array(
  8. 'status' => 'success',
  9. 'message'=> $arr, //仅用于调试,将替换为成功消息
  10. ));
  11. }else {
  12. echo json_encode(array(
  13. 'status' => 'error',
  14. 'message'=> 'error message'
  15. ));
  16. }
英文:

The problem one is that response is in JSON. So you have to convert it into JS object and then you can get status. Here is the updated Code:-

  1. function applyPromo(){
  2. var promo = $("#promo-code-value").val().trim();
  3. console.log(promo);
  4. $.ajax({
  5. url:"get-promo.php",
  6. type: "POST",
  7. data:{ getpromo : promo},
  8. success: function(response){
  9. let response = JSON.parse(response);
  10. console.log("%j", response);
  11. console.log(response.status);
  12. if(response.status === "success"){
  13. swal("Good job!!",
  14. "Promo code applied Successfully!",
  15. "success"
  16. ).then(() => {
  17. reloadPage();
  18. });
  19. }else{
  20. swal("Humm...", "Promo code no longervalid","error");
  21. }
  22. }
  23. })
  24. }

The second problem is that, you are not using session_start(). Here is the updated code:-

  1. if(isset($arr)){
  2. //Add array to session variable
  3. //** Only one code can be apply at the time . will override
  4. session_start();
  5. $_SESSION['promo-code'] = $arr;
  6. //Success response
  7. echo json_encode(array(
  8. 'status' => 'success',
  9. 'message'=> $arr, //For debugging only , will be replaced to success message
  10. ));
  11. }else {
  12. echo json_encode(array(
  13. 'status' => 'error',
  14. 'message'=> 'error message'
  15. ));
  16. }

huangapple
  • 本文由 发表于 2023年5月14日 05:31:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定