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

使用AJAX调用Java Web Service插入MySQL失败,POSTMAN正常求助

Hey there! It sounds like your backend service is working perfectly since Postman tests pass, so the issue is almost certainly with how your AJAX request is configured. Let's walk through the most common fixes to get this working:

Troubleshooting Steps for Your AJAX Issue

1. Block the Form's Default Submission Behavior

Your form has the ID submit—if you don't stop the browser's default form submission action, the page will refresh immediately, cutting off your AJAX request and leading to that confusing "false" message.

Add preventDefault() to your form submit handler:

document.getElementById('submit').addEventListener('submit', function(e) {
  // Stop the browser from reloading the page
  e.preventDefault();
  
  // Your AJAX logic goes here
});

2. Match the Request Format to Your Backend's Expectations

Postman works, so your Java service is expecting a specific data format. If your AJAX sends data in a different format (or misspells parameter names), the service will fail silently or return errors.

Option 1: Send Form Data (for x-www-form-urlencoded endpoints)

Use FormData to mirror how Postman sends form data:

const formElement = document.getElementById('submit');
const formData = new FormData(formElement);

fetch('your-web-service-endpoint-url', {
  method: 'POST',
  body: formData
})
.then(response => response.text()) // Use response.json() if your backend returns JSON
.then(result => {
  console.log('Response:', result);
  // Handle your result here
})
.catch(error => console.error('Request failed:', error));

Option 2: Send JSON (if your backend expects JSON payloads)

If your Java service requires JSON, convert the form data to a JSON string:

const formElement = document.getElementById('submit');
const formData = new FormData(formElement);
const payload = Object.fromEntries(formData.entries());

fetch('your-web-service-endpoint-url', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(payload)
})
// Same then/catch logic as above

3. Check for CORS Restrictions

If your Add.jsp page is hosted on a different domain/port than your Java Web Service, browsers will block the AJAX request due to CORS rules (Postman doesn't enforce CORS, which is why it works).

Fix this by adding CORS headers to your Java service. You can do this via a filter or directly in your endpoint:

// Example in a Java servlet/controller
response.setHeader("Access-Control-Allow-Origin", "http://your-frontend-domain.com"); // Replace with your actual frontend URL
response.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");

Note: Avoid using * for Access-Control-Allow-Origin in production—it’s a security risk.

4. Double-Check the AJAX Request URL

Make sure the URL in your AJAX code matches exactly what you used in Postman. Relative paths can be tricky: if Postman uses http://localhost:8080/api/add, use /api/add (with a leading slash) if your page is at http://localhost:8080/, or the full URL if it's cross-domain.

5. Use Browser DevTools to Diagnose

Open your browser's Developer Tools (F12) and check two key tabs:

  • Network: Locate your POST request, then look at the status code (404 means wrong URL, 400 means invalid params, 500 means backend crash), request payload, and response content.
  • Console: Look for error messages like CORS blocks or JavaScript syntax mistakes.

These tools will give you concrete clues about what's breaking the request.


内容的提问来源于stack exchange,提问作者Hasitha Jayawardana

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:30:05