gpt4 book ai didi

javascript - jquery ajax "SyntaxError: Unexpected end of input"有效 JSON

转载 作者:行者123 更新时间:2023-12-03 10:26:05 24 4
gpt4 key购买 nike

我的 PHP 返回的 JSON 是:

{"success":0,"message":"错误:没有传递实体ID!"}

我的 javascript 仍然告诉我“SyntaxError:输入意外结束”。

PHP:

    ...

//check if an image id was passed for removal in the POST data
if ( isset($_POST["entityId"])) {
$entityId = $_POST["entityId"];
}
else {
//setup response json
$resp = array();
$resp['success'] = 0;
$resp['message'] = "Error: No entityId passed!";

header('Content-Type: application/json');
echo json_encode($resp);
}

...

JS:

             // send xhr request
$.ajax({
dataType: 'json',
type: $theForm.attr('method'),
url: $theForm.attr('action'),
data: $theForm.serialize(),
success: function(data) {
//backend returns a json with variable success either true or false.
resp = data;
if(resp.success == 1) {
//render gallery anew
}
else {
console.log(data);
if (data.message) {
$(self).find('.VanillaGallery-overlayContentWrapper').html(data.message);
}
else {
$(self).find('.VanillaGallery-overlayContentWrapper').html('Oops! Något gick fel, felmeddelande saknas dock.');
}
}

},
error: function(xhr, status, text) {
$(self).find('.VanillaGallery-overlayContentWrapper').html('Oops! Något gick fel...<br />'+text);
}
});

很奇怪。我的 header 会有所不同,具体取决于请求是通过 ajax 发出还是直接在浏览器中作为普通单独请求(而不是通过 ajax):

标题通过 ajax 看起来像这样:

Connection:Keep-Alive
Content-Length:0
Content-Type:text/html
Date:Wed, 01 Apr 2015 17:48:26 GMT
Keep-Alive:timeout=5, max=97
Server:Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8zc DAV/2 PHP/5.5.3
X-Pad:avoid browser bug
X-Powered-By:PHP/5.5.3

直接从浏览器地址栏通过此操作:

Connection:Keep-Alive
Content-Length:52
Content-Type:application/json
Date:Wed, 01 Apr 2015 17:42:23 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8zc DAV/2 PHP/5.5.3
X-Powered-By:PHP/5.5.3

最佳答案

好的,所以一旦你用新的眼光来看它就很明显了......在ajax请求中传递了一个entityId(从示例代码中不明显,因为没有显示表单数据......),因此第一个上面 PHP 代码中的 IF 条件计算结果为 true。在该子句中,没有输出,没有任何类型的回显。这就是为什么我得到“意外的输入结束”。

至于通过直接在浏览器地址栏中运行来测试它,这样 PHP 当然会落在我上面 PHP 代码的 else 括号中,并且实际上给出响应,因为根本没有 POST 数据就这样...

抱歉占用了您的时间,有时候就是太累了...

关于javascript - jquery ajax "SyntaxError: Unexpected end of input"有效 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com