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

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

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

function applyPromo(){
    var promo = $("#promo-code-value").val().trim();
    console.log(promo);
    $.ajax({
        url: "get-promo.php",
        type: "POST",
        data: { getpromo: promo },
        success: function (response) {
            console.log("%j", response);
            console.log(response.status);
            if (response.status === "success") {
                swal("Good job!!", 
                    "Promo code applied Successfully!",
                    "success"
                    ).then(() => {
                        reloadPage();
                    });
            } else {
                swal("Humm...", "Promo code no longer valid", "error");
            }
        }
    })
}

PHP

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

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

我尝试将 "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

	function applyPromo(){
		var promo = $("#promo-code-value").val().trim();
		console.log(promo);
		$.ajax({
			url:"get-promo.php",
			type: "POST",
			data:{ getpromo : promo},
			success: function(response){
				console.log("%j", response);
				console.log(response.status);
				if(response.status === "success"){
					swal("Good job!!", 
						"Promo code applied Successfully!",
						"success"
						).then(() => {
							reloadPage();
						});
					}else{
					swal("Humm...", "Promo code no longervalid","error");
					}	
				}
			})
		}

PHP

if(isset($arr)){
        //Add array to session variable 
        //** Only one code can be apply at the time . will override
        $_SESSION['promo-code'] = $arr; 
        //Success response 
        echo json_encode(array(
            'status' => 'success',
            'message'=> $arr, //For debugging only , will be replaced to success message 
        ));
    }else {
        echo json_encode(array(
            'status' => 'error',
            'message'=> 'error message'
        ));
    }

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对象,然后您就可以获取状态。以下是更新后的代码:

function applyPromo(){
   var promo = $("#promo-code-value").val().trim();
   console.log(promo);
    $.ajax({
      url:"get-promo.php",
      type: "POST",
      data:{ getpromo : promo},
      success: function(response){
         let response = JSON.parse(response);
         console.log("%j", response);
         console.log(response.status);
         if(response.status === "success"){
            swal("Good job!!", 
              "Promo code applied Successfully!",
              "success"
            ).then(() => {
               reloadPage();
            });
         }else{
            swal("Humm...", "Promo code no longer valid","error");
         }   
      }
    })
  }

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

if(isset($arr)){
  //将数组添加到会话变量
  //** 一次只能应用一个代码,将覆盖
  session_start();
  $_SESSION['promo-code'] = $arr; 
  //成功响应 
  echo json_encode(array(
     'status' => 'success',
     'message'=> $arr, //仅用于调试,将替换为成功消息 
  ));
}else {
  echo json_encode(array(
    'status' => 'error',
    'message'=> 'error message'
  ));
}
英文:

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:-

function applyPromo(){
   var promo = $("#promo-code-value").val().trim();
   console.log(promo);
    $.ajax({
      url:"get-promo.php",
      type: "POST",
      data:{ getpromo : promo},
      success: function(response){
         let response = JSON.parse(response);
         console.log("%j", response);
         console.log(response.status);
         if(response.status === "success"){
            swal("Good job!!", 
              "Promo code applied Successfully!",
              "success"
            ).then(() => {
               reloadPage();
            });
         }else{
            swal("Humm...", "Promo code no longervalid","error");
         }   
      }
    })
  }

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

if(isset($arr)){
  //Add array to session variable 
  //** Only one code can be apply at the time . will override
  session_start();
  $_SESSION['promo-code'] = $arr; 
  //Success response 
  echo json_encode(array(
     'status' => 'success',
     'message'=> $arr, //For debugging only , will be replaced to success message 
  ));
}else {
  echo json_encode(array(
    'status' => 'error',
    'message'=> 'error message'
  ));
}

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:

确定