简体   繁体   English

收到来自 AJAX POST 请求的 JSON 响应?

[英]Receive JSON response from AJAX POST request?

I'm setting up a system for users to upload profile pictures to the server.我正在建立一个系统,供用户将个人资料图片上传到服务器。 These profile pictures are cropped with Croppie, then sent with a POST request for processing to a PHP file.这些个人资料图片使用 Croppie 裁剪,然后与 POST 请求一起发送到 PHP 文件进行处理。

This PHP file receives a response from the server if the image was accepted, or if there was an error.如果图像被接受或出现错误,此 PHP 文件会从服务器接收响应。

I'm having difficulty passing this JSON response back to an AJAX variable, so I can show it to end-users.我很难将此 JSON 响应传递回 AJAX 变量,因此我可以将其展示给最终用户。 I've set up a with the ID of "json" where the response should be shown.我已经设置了一个 ID 为“json”的,应该在其中显示响应。 However, all that is being displayed is 'undefined'.但是,显示的所有内容都是“未定义的”。

When displaying the 'response' variable without.msg, it displays the image URI - so it doesn't appear to be taking the requests made in the PHP script.当显示没有.msg 的“响应”变量时,它会显示图像 URI - 因此它似乎没有接受 PHP 脚本中发出的请求。

Here's what I've tried:这是我尝试过的:

AJAX AJAX

$('.crop_image').on('click', function(ev) {
  $image_crop.croppie('result', {
    type: 'canvas',
    size: 'viewport'
  }).then(function(response) {
    $.ajax({
      type: 'POST',
      url: "core/account-mechanisms/push-profile-test.php?action=upload",
      data: {
        "image": response
      },
    });
    $("#json").html('<div style="margin-top:15px;" class="alert alert-success">'
      + response.msg + '</div>');
  })
});

PHP (start of script) PHP(脚本开始)

# Mechanism to upload image
if (isset($_POST['image'])) { # Check if image data is being sent by POST
  $c_finalCheck = true;
  $cropped_image = $_POST['image']; # Assign cropped_image from data received
  $image_array_1 = explode(";", $cropped_image); # Create image with correct encoding
  $image_array_2 = explode(",", $image_array_1[1]);
  $cropped_image = base64_decode($image_array_2[1]);

  $c_imgValid = getimagesize($c_fileCheck); # Set result of image validity and size check
  if ($c_fileCheck == false) { # Run image validity check with response
    $response['msg'] = 'Sorry, your image isn\'t valid. Please try again.';
    $c_finalCheck = false;
    header("Content-Type:application/json");
    echo json_encode($response);
  }

you are sending the Ajax Post to your Server, but you are not using the Ajax response.您正在将 Ajax 帖子发送到您的服务器,但您没有使用 Ajax 响应。 Instead you are displaying the response of your croppie call.相反,您正在显示您的作物呼叫的响应。

You need to do something like this:你需要做这样的事情:

  $('.crop_image').on('click', function(ev) {
                        $image_crop.croppie('result', {
                            type: 'canvas',
                            size: 'viewport'
                        }).then(function(response) {
                            $.ajax({
                                type: 'POST',
                                url: "core/account-mechanisms/push-profile-test.php?action=upload",
                                data: {
                                    "image": response
                                },
                            }).done(function(data) {
                             $("#json").html('<div style="margin-top:15px;" class="alert alert-success">' + response + '</div>');
});
                        })
                    });

I did not try it but it should give you the right direction.我没有尝试过,但它应该给你正确的方向。

Here the jquery Api reference: Ajax这里 jquery Api 参考: Ajax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM