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

9次AJAX调用仅前3次成功,剩余触发500内部服务器错误求助

Hey there, let's break down why your AJAX requests are failing in production and walk through practical fixes!

What's Causing the 500 Errors?

Your issue is almost certainly tied to concurrent request limits—both on the browser and server side:

  1. Browser Concurrency Limits: Most browsers restrict concurrent HTTP requests to the same domain (usually 4-6 at a time). Local testing works because your server is fast and can handle the load, but production networks have more latency, so extra requests get blocked or time out.
  2. Server-Side Limits: Your backend (Apache, NGINX, PHP-FPM, etc.) likely has a cap on simultaneous connections. Firing 9 requests at once can trigger this limit, leading to 500 errors.
  3. Sync AJAX Blocking: Your getBoardAssignChecked function uses async:false, which blocks the browser's main thread. This can delay or break subsequent requests.

Instead of firing all 9 requests at once, run them one after another using Promises. This stays within concurrency limits and ensures each request completes before the next starts.

First, update each AJAX function to return a Promise:

function getBoardCard(id) {
  return new Promise((resolve, reject) => {
    $.ajax({
      type: "POST",
      url: "board/getBoardCard",
      data: {id: id},
      dataType: "json",
      success: (response) => {
        // Your existing DOM update logic here
        $.each(response, (idx, item) => {
          $("#modalCardTitle").text(item.cardTitle);
          $("#modalListTitle").text("in list " + item.listTitle);
          // ... rest of your updates
        });
        resolve(); // Signal request is done
      },
      error: (xhr, _, thrownError) => {
        console.error("getBoardCard failed:", xhr.status, thrownError);
        reject(new Error(thrownError));
      }
    });
  });
}

// Repeat this Promise pattern for getBoardMember, getBoardLabelCard, and all other getXXX functions

Then rewrite your main function to chain the requests:

function ajaxModalCard(id) {
  $("#hiddenCardId").val(id);
  
  getBoardCard(id)
    .then(() => getBoardMember(id))
    .then(() => getBoardLabelCard(id))
    .then(() => getStartDate(id))
    .then(() => getDueDate(id))
    .then(() => getChecklist(id))
    .then(() => getComment(id))
    .then(() => getAttachment(id))
    .then(() => getMoveCard(id))
    .then(() => {
      // All requests done—show the modal
      $("#changeArchive").show();
      $("#changeSend").hide();
      $("#modal3").openModal();
    })
    .catch((err) => {
      console.error("One of the requests failed:", err);
    });
}

Fix 2: Merge Requests into a Single Backend Endpoint

The most efficient long-term fix is to create one backend endpoint that returns all the data you need in one go (e.g., board/getCardFullDetails). This cuts down on HTTP overhead entirely and avoids concurrency issues.

Frontend example:

function ajaxModalCard(id) {
  $("#hiddenCardId").val(id);
  const boardId = $("#hiddenBoardId").val();

  $.ajax({
    type: "POST",
    url: "board/getCardFullDetails",
    data: {id: id, boardId: boardId},
    dataType: "json",
    success: (fullData) => {
      // Handle each data type separately
      handleCardData(fullData.card);
      handleMemberData(fullData.members);
      handleLabelData(fullData.labels);
      // ... handle dates, checklists, comments, etc.

      // Show modal once all data is processed
      $("#changeArchive").show();
      $("#changeSend").hide();
      $("#modal3").openModal();
    },
    error: (xhr, _, thrownError) => {
      console.error("Failed to load card details:", xhr.status, thrownError);
    }
  });
}

// Helper functions to process each data set
function handleCardData(card) {
  $("#modalCardTitle").text(card.cardTitle);
  $("#modalListTitle").text("in list " + card.listTitle);
  // ... rest of your card updates
}

On the backend, you'd modify your code to fetch all required data (card info, members, labels, etc.) in one database query batch and return it as a single JSON object.


Fix 3: Remove Synchronous AJAX

Your getBoardAssignChecked uses async:false, which blocks the browser and can break request sequencing. Rewrite it to use async/await or Promises:

function getBoardAssignChecked(userId, cardId) {
  return new Promise((resolve, reject) => {
    $.ajax({
      type: "POST",
      url: "board/getBoardAssignChecked",
      data: {userId: userId, cardId: cardId},
      success: (response) => resolve(response),
      error: (xhr, _, thrownError) => {
        console.error("getBoardAssignChecked failed:", xhr.status, thrownError);
        reject(new Error(thrownError));
      }
    });
  });
}

// Update getBoardMember to use async/await for this check
async function getBoardMember(id) {
  return new Promise((resolve, reject) => {
    const boardId = $("#hiddenBoardId").val();
    $.ajax({
      type: "POST",
      url: "board/getBoardMember",
      data: {boardId: boardId},
      dataType: "json",
      success: async (members) => {
        $("#assignMembers").empty();
        $("#iconMember").empty().append("<h6><b>Members</b></h6>");

        for (const member of members) {
          if (member.memberStatus === "1") {
            const checked = await getBoardAssignChecked(member.userId, $("#hiddenCardId").val());
            const name = getNameUser(member.userId);
            // Build your member HTML here using the checked value
            // ...
          }
        }
        resolve();
      },
      error: (xhr, _, thrownError) => {
        console.error("getBoardMember failed:", xhr.status, thrownError);
        reject(new Error(thrownError));
      }
    });
  });
}

Quick Debugging Tip

Check your production server logs (PHP error logs, NGINX access/error logs) for the exact cause of the 500 error. Sometimes it's not concurrency—maybe a missing environment variable or database connection issue that only shows up under load.

内容的提问来源于stack exchange,提问作者Michael Chandra Lrc

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:54:03