简体   繁体   English

Ajax选择带有实时消息的帖子

[英]Ajax select post with real-time message

I'm trying to get real-time message and response from this POST, but doesn't work for me. 我正在尝试从此POST获取实时消息和响应,但是不适用于我。 I want to display in this Ajax text from timezone.php - error or success Where I made mistake? 我想显示来自timezone.php的此Ajax文本- errorsuccess哪里出错了?

HTML 的HTML

<form id="timezone_form">
  <select name="timezone_select">
    <option value="1">test 1</option>
    <option value="2">test 2</option>
  </select>

  <input type="submit" value="Submit"/>
</form>

<div id="settings_ajax_message"></div>

JS JS

$(document).ready(function() {

  $("#timezone_form").submit(function() {

    $.ajax({
      type: "POST",
      url: "timezone.php",
      data: $(this).serialize()
    })

    .done(function(data) {
      $("#settings_ajax_message").html(data);
    })

    .fail(function() {
      $("#settings_ajax_message").html(data);
    });

    return false;
  });

});

timezone.php timezone.php

<?php

  $timezone = $_POST['timezone_select'];
  if ($timezone == 1) {
    echo '<div class="ch-mes-fixed-warning-animation"><div class="ch-mes-warning">error</div></div>';
  } else {
    echo '<div class="ch-mes-fixed-success"><div class="ch-mes-success">success</div></div>';
  }

?>

First thing, you have to understand why your fail function would be called: 首先,您必须了解为什么调用失败函数:

$(document).ready(function(e) {
  // Prevent the form from actually submitting to the server
  e.preventDefault();

  $("#timezone_form").submit(function() {

    $.ajax({
      type: "POST",
      url: "timezone.php",
      data: $(this).serialize()
    })

    // Status code 200-299 was returned. The data was successfully returned from the server.
    // PHP by default will always return 200 (Which signifys that the server processed the data, and is returning it)
    .done(function(data) {
      $("#settings_ajax_message").html(data);
    })

    // Status 300+ was returned from the server. This usually indicates an error on the server (ie. 404 not found, or 500 server error)
    .fail(function() {
      $("#settings_ajax_message").html(data);
    });

    return false;
  });

});

You have 2 ways to handle this. 您有2种方法可以解决此问题。 1, have your server actually return an error (Usually the preferred route for an API): 1,让您的服务器实际返回错误(通常是API的首选路由):

header("HTTP/1.0 422 Error");

In most cases, you should catch bad errors with .fail like if your server craps out. 在大多数情况下,您应该使用.fail捕获严重错误,就像服务器.fail一样。 Have your server return 200 (Which will process the done function), but check for an error property: 让您的服务器返回200(这将处理完函数),但是请检查错误属性:

<?php
  // Tell javascript this is a json response:
  header("Content-type: application/json");
  $timezone = $_POST['timezone'];
  if ($timezone == 1) {
    echo json_encode(['error' => true, 'message' => '<div class="ch-mes-fixed-warning-animation"><div class="ch-mes-warning">error</div></div>']);
  } else {
    echo json_encode(['error' => false, 'message' => '<div class="ch-mes-fixed-success"><div class="ch-mes-success">success</div></div>');
  }

?>

And your ajax: 还有你的ajax:

$(document).ready(function() {

  $("#timezone_form").submit(function() {

    $.ajax({
      type: "POST",
      url: "timezone.php",
      dataType: 'json', //IMPORTANT! PROCESS AS JSON
      data: $(this).serialize()
    })

    .done(function(data) {
       if (data.error) {
         console.log(data);
         //Do something with the error
         $("#settings_ajax_message").html(data.message);
       } else {
         //No error. data.message will be the message
       }
    })

    .fail(function() {
      $("#settings_ajax_message").html(data);
    });

    return false;
  });

At first, your field name is timezone_select , look here: <select name="timezone_select"> . 首先,您的字段名称是timezone_select ,请看此处: <select name="timezone_select"> In PHP you checking another name: $timezone = $_POST['timezone']; 在PHP中,您检查另一个名称: $timezone = $_POST['timezone'];

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

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