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

使用NodeJS对接Coinbase API时遭遇Cross-Origin Request Blocked错误求助

对接Coinbase API时遇到CORS错误及代码问题求助

错误信息

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/create-payment. (Reason: CORS request did not succeed). Status code: (null).

已尝试的解决方法

  • 下载CORS插件但无效
  • 改用127.0.0.1替代localhost
  • 更换端口

前端JavaScript代码

const button = document.getElementById('order');

button.addEventListener('click', event => { 
  const form = document.getElementById('payment-form');

  event.preventDefault(); // Prevent the form from submitting to the server 
  form.submit(); 
  const price = "<?php echo $price; ?>"; 
  const amount = document.getElementById('quantitymills').value;
  const total_price = document.getElementById('inputd').value; 
  const fname = document.getElementById('fname').value; 
  const email = document.getElementById('email').value;

  fetch('http://localhost:3000/create-payment', { 
    method: 'POST', 
    body: JSON.stringify({ price }), 
    body: JSON.stringify({ amount }), 
    body: JSON.stringify({ total_price }), 
    body: JSON.stringify({ name }),
    body: JSON.stringify({ email }), 
    headers: { 
      'Content-Type': 'application/json',
    }
  }) 
  .then(response => response.json())
  .then(payment => { 
    // Do something with the payment details
    console.log(payment);
  }) 
  .catch(error => console.error(error)); 
});

后端Node.js代码

const express = require('express'); 
const axios = require('axios');
const bodyParser = require('body-parser');
const mysql = require('mysql2'); 
const app = express();

// Parse application/x-www-form-urlencoded requests 
app.use(bodyParser.urlencoded({ extended: false }));

// Connect to the database 
const db = mysql.createConnection({ 
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'coinbase' 
});

// Create a payment
app.post('http://localhost:3000/create-payment', async (req, res) => { 
  try { 
    // Make an API request to create the payment 
    const response = await axios.post( 
      'https://api.commerce.coinbase.com/charges',
      { 
        name: 'Product one', 
        description: 'Product one',
        local_price: { 
          amount: req.body.total_price,
          currency: 'USD' 
        }, 
        metadata: {}, 
        pricing_type: 'fixed_price',
        redirect_url: 'http://localhost/index.php' 
      },
      { 
        headers: { 
          'X-CC-Api-Key': 'myapikey' 
        } 
      }
    );

    // Store the payment URL in the database
    const payment = {
      url: response.data.hosted_url,
      amount: req.body.total_price,
      currency: 'USD',
      status: 'pending'
    };
    db.query('INSERT INTO payments SET ?', payment, (error, result) => {
      if (error) throw error;
      res.send(payment);
    });

  } catch (error) { 
    console.error(error); 
  } 
});

app.listen(3000, () => { 
  console.log('Server listening on port 3000'); 
});

我是编程新手,不确定代码里还有哪些错误,以上方法都试过但问题仍未解决,恳请各位提供帮助!


内容的提问来源于stack exchange,提问作者Im a noob

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 06:20:41