简体   繁体   English

条带意外标记 < 在 JSON 在 position 0

[英]Stripe Unexpected token < in JSON at position 0

I am getting this error, it points to client.js line 21. It is the second.then of the fetch(create.php).我收到这个错误,它指向 client.js 的第 21 行。它是 fetch(create.php) 的第二个。

The first response returns 200. So, not sure how to fix it.第一个响应返回 200。因此,不确定如何修复它。 The whole code so far is as extracted from demo instructions.到目前为止,整个代码都是从演示说明中提取的。 https://stripe.com/docs/payments/integration-builder https://stripe.com/docs/payments/integration-builder

See browser console info:查看浏览器控制台信息:

jquery-migrate.min.js?ver=3.3.2:2 JQMIGRATE: Migrate is installed, version 3.3.2
?ver=3.0.0:1 You may test your Stripe.js integration over HTTP. However, live Stripe.js integrations must use HTTPS.
value @ ?ver=3.0.0:1
Ec @ ?ver=3.0.0:1
Sc @ ?ver=3.0.0:1
(anonymous) @ client.js:3
client.js:18 Response {type: "basic", url: "http://amore-paraiso.local/wp-content/plugins/sm-amore-stripe/create.php", redirected: false, status: 200, ok: true, …}body: (...)bodyUsed: trueheaders: Headers {}ok: trueredirected: falsestatus: 200statusText: "OK"type: "basic"url: "http://amore-paraiso.local/wp-content/plugins/sm-amore-stripe/create.php"__proto__: Response
content-tss.js:1 TSS: content-tss.js loaded:  https://js.stripe.com/v3/m-outer-59cdd15d8db95826a41100f00b589171.html#url=http%3A%2F%2Famore-paraiso.local%2Fexperiences%2Fbride-groom%2F%3F&title=Bride%20%26%20Groom%20%E2%80%93%20Amore%20Paraiso&referrer=http%3A%2F%2Famore-paraiso.local%2Fexperiences%2Fbride-groom%2F%3F&muid=bb6c8b5b-6e39-4451-91e7-10092d15ec8824d547&sid=3aa75c2f-71f2-493c-ae1b-8f76050ebb800df509&version=6&preview=false
content-ads.js:1 INS: content-ads.js loaded:  https://js.stripe.com/v3/m-outer-59cdd15d8db95826a41100f00b589171.html#url=http%3A%2F%2Famore-paraiso.local%2Fexperiences%2Fbride-groom%2F%3F&title=Bride%20%26%20Groom%20%E2%80%93%20Amore%20Paraiso&referrer=http%3A%2F%2Famore-paraiso.local%2Fexperiences%2Fbride-groom%2F%3F&muid=bb6c8b5b-6e39-4451-91e7-10092d15ec8824d547&sid=3aa75c2f-71f2-493c-ae1b-8f76050ebb800df509&version=6&preview=false
VM353:4 hosted page injected
(index):1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
Promise.then (async)
(anonymous) @ client.js:21
content-tss.js:1 TSS: content-tss.js loaded:  https://m.stripe.network/inner.html#url=http%3A%2F%2Famore-paraiso.local%2Fexperiences%2Fbride-groom%2F%3F&title=Bride%20%26%20Groom%20%E2%80%93%20Amore%20Paraiso&referrer=http%3A%2F%2Famore-paraiso.local%2Fexperiences%2Fbride-groom%2F%3F&muid=bb6c8b5b-6e39-4451-91e7-10092d15ec8824d547&sid=3aa75c2f-71f2-493c-ae1b-8f76050ebb800df509&version=6&preview=false
content-ads.js:1 INS: content-ads.js loaded:  https://m.stripe.network/inner.html#url=http%3A%2F%2Famore-paraiso.local%2Fexperiences%2Fbride-groom%2F%3F&title=Bride%20%26%20Groom%20%E2%80%93%20Amore%20Paraiso&referrer=http%3A%2F%2Famore-paraiso.local%2Fexperiences%2Fbride-groom%2F%3F&muid=bb6c8b5b-6e39-4451-91e7-10092d15ec8824d547&sid=3aa75c2f-71f2-493c-ae1b-8f76050ebb800df509&version=6&preview=false

Most likely scenario is that your create.php is encountering an error and returning an error page as HTML (hence the < at position 0).最可能的情况是您的create.php遇到错误并返回错误页面 HTML(因此<位于 position 0)。 You need to debug your create.php to understand where it is failing, then correct that.您需要调试您的create.php以了解它失败的地方,然后更正它。 Check your Stripe developer logs to see if the API request is made successfully.检查您的 Stripe开发者日志,看看 API 请求是否成功。

I've had simular problems passing data from php to js.我在将数据从 php 传递到 js 时遇到了类似的问题。 Instead of immediately parsing the data, just do console.log(this.responseText);不要立即解析数据,只需执行console.log(this.responseText); and it will show you the content.它会告诉你内容。 Normally it's an error in your php code and it will tell you where the error is and what's causing it for you to fix通常这是您的 php 代码中的错误,它会告诉您错误在哪里以及导致错误的原因,以便您修复

I've encountered this error a few times.我遇到过几次这个错误。 You aren't actually getting back JSON as a response, and so the attempt to parse that response as JSON is failing.您实际上并没有返回 JSON 作为响应,因此将该响应解析为 JSON 的尝试失败了。

If you are doing something like result.json() you can instead log result.text() and have a look at what is actually being returned.如果您正在执行类似result.json()的操作,您可以改为记录result.text()并查看实际返回的内容。

PHP has a habit of catching people out and returning HTML rather than JSON. PHP有抓人出来返回HTML而不是JSON的习惯。

Here is a massively popular Stack Overflow thread on the topic:这是一个关于该主题的广受欢迎的 Stack Overflow 线程:

SyntaxError: Unexpected token < in JSON at position 0 语法错误:JSON 中的意外标记 < position 0

Edit because reviewed:编辑因为审查:

Your error is appearing here你的错误出现在这里

  .then(function(result) {
    return result.json();
  })

So if you want to make sure this is JSON you can check the response content type like below.因此,如果您想确保这是 JSON,您可以检查响应内容类型,如下所示。 Taken from MDN摘自MDN

fetch("/create-payment-intent", {
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify(purchase)
})
  .then(result => {
     const contentType = result.headers.get('content-type');
     if (!contentType || !contentType.includes('application/json')) {
       // Or do something else with the result to get it into JSON
       throw new TypeError("Oops, we haven't got JSON!");
     }
     return result.json();
  }

It turned out the error was on the require './stripe-php/init.php';原来错误是在require './stripe-php/init.php'; on the create.php file.create.php文件上。

As I am developing it with Wordpress I had it as require plugin_dir_url(__FILE__). 'stripe-php/init.php';当我用 Wordpress 开发它时,我把它作为require plugin_dir_url(__FILE__). 'stripe-php/init.php'; require plugin_dir_url(__FILE__). 'stripe-php/init.php'; . .

Summary:概括:

Make sure the path to require stripe-php/init.php is correct, as below:确保require stripe-php/init.php路径正确,如下:

require './stripe-php/init.php';

Thanks to @jason who recommended checking the Network tab.感谢@jason建议检查网络选项卡。

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

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