You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ajax中值变为null问题求助:更新收货城市文本框值后异常

Troubleshooting Your Ajax Null Response Issue

Let’s walk through the key issues in your code that are likely causing the null Ajax response, along with fixes:

1. You’re Not Outputting the JSON Response

The biggest oversight here is that you’re building the $json array but never sending it back to the client. Without echoing the JSON data, your Ajax call will receive an empty (or null) response every time.

Add this line at the end of your POST block to return the data:

echo json_encode($json);
exit; // Optional but stops unnecessary script execution after sending the response

2. Incomplete SQL Query

Your UPDATE query cuts off at WHERE order... — this will throw a SQL syntax error, which probably halts the script before it can output anything. You need to finish the WHERE clause to target the correct order. For example, if using an order_id parameter:

$this->db->query("UPDATE " . DB_PREFIX . "order SET shipping_city = '" . $this->db->escape($this->request->post['shipping_city']) . "' WHERE order_id = '" . (int)$this->request->post['order_id'] . "'");

(Casting order_id to int is a quick security win since it should always be a numeric value.)

3. Missing Input Validation & Error Handling

  • Check for required POST parameters: If shipping_city (or your order identifier) isn’t sent in the request, accessing $this->request->post['shipping_city'] could trigger an error. Add checks like:
    if (!isset($this->request->post['shipping_city']) || trim($this->request->post['shipping_city']) === '') {
        $json['error'] = 'Shipping city is required';
        echo json_encode($json);
        exit;
    }
    
  • Verify database query success: Add a check to confirm the UPDATE ran properly. Most database classes have a method to count affected rows. For example:
    $this->db->query(...);
    if ($this->db->countAffected() == 0) {
        $json['error'] = 'No order found or no changes made';
    }
    

4. Set the Correct Content-Type Header

It’s good practice to tell the client you’re sending JSON, so it parses the response correctly. Add this right before echoing the JSON:

header('Content-Type: application/json');

Updated Full Function Example

Here’s how your fixed function might look with all these improvements:

public function updateshippingcity() {
    if ($this->request->server['REQUEST_METHOD'] == 'POST') {
        header('Content-Type: application/json');
        $json = [];

        // Validate input
        if (!isset($this->request->post['shipping_city']) || trim($this->request->post['shipping_city']) === '') {
            $json['error'] = 'Please provide a shipping city';
            echo json_encode($json);
            exit;
        }

        if (!isset($this->request->post['order_id']) || !is_numeric($this->request->post['order_id'])) {
            $json['error'] = 'Invalid order ID';
            echo json_encode($json);
            exit;
        }

        // Execute database update
        $this->db->query("UPDATE " . DB_PREFIX . "order SET shipping_city = '" . $this->db->escape($this->request->post['shipping_city']) . "' WHERE order_id = '" . (int)$this->request->post['order_id'] . "'");

        // Check if update succeeded
        if ($this->db->countAffected() > 0) {
            $json['new_shipping_city'] = $this->request->post['shipping_city'];
            $json['success'] = true;
        } else {
            $json['error'] = 'Failed to update shipping city';
        }

        echo json_encode($json);
        exit;
    }
}

Also double-check that your Ajax call is sending all required POST data (like shipping_city and order_id) and is configured to expect a JSON response.

内容的提问来源于stack exchange,提问作者Ask Xah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:28:31