jQuery/PHP-POST-Forbidden(403)当试图在我的JSON数据中发送完整的URL时

jQuery/PHP- POST Forbidden (403) when trying to send a complete URL in my JSON data

本文关键字:数据 URL JSON 我的 PHP-POST-Forbidden jQuery      更新时间:2023-10-15

我正在制作一个有趣的卡片生成器,学习如何从输入中收集数据并写入数据库。

当尝试发送这段简单的JSON数据时,我从PHP脚本中得到POST Forbidden(403)错误。

这是我正在处理的jsonlint.com验证对象,它是从输入元素和javascript-生成的

{
    "name": "Transfer Power",
    "layout": "cic",
    "artwork": "http://static.tvtropes.org/pmwiki/pub/images/Voyager_shield.jpg",
    "rarity": "common",
    "cost": "3",
    "text": "Until the start of your next turn, target friendly Ship gains Shield +X and Damage -X where X is the Ship's current Damage."
}

据我所知,I should be able to send the complete artwork url没有任何格式,但POST只有在我删除url的方案时才能工作。我希望它是简单的。这是代码-

var name = $('#name').val();
var layout = type;
var artwork = $('#artwork').val();
var rarity = $('#rarity').val();
var cost = $('#cost').val();
var type = $('#type').val();
var text = $('#text').val();
var card = {};
card.name = name;
card.layout = layout;
card.artwork = artwork;
card.rarity = rarity;
card.cost = cost;
card.text = text;
$.post('writecard.php', card, function(data){
    console.log(data);
});

和超简单的PHP回声来重新验证我的数据-

<?php
    $raw = $_POST;
    foreach($raw as $key => $val){
        echo $key . ': ' . $val . ' -- ';
    }
?>

再次:当我删除URL的方案时,它成功地发送POST并返回数据。

另一个值得关注的问题是:我如何确保我发送的是整数而不是字符?"cost": "3",是否应该不显示为"cost": 3,

感谢阅读。:]

根据您的示例使用writecard.php时,以下index.html:

<html>
    <script src="jquery-1.10.2.min.js" ></script>
    <body>
        <input id="name" value="Transfer Power" />
        <br/>
        <input id="layout" value="cic" />
        <br/>
        <input id="artwork" value="http://static.tvtropes.org/pmwiki/pub/images/Voyager_shield.jpg" />
        <br/>
        <input id="rarity" value="common" />
        <br/>
        <input id="cost" value="3" />
        <br/>
        <input id="type" value="unknown" />
        <br/>
        <input id="text" value="Until the start of your next turn, target friendly Ship gains Shield +X and Damage -X where X is the Ship's current Damage." />
    </body>
    <script>
        var name = $('#name').val();
        var layout = type;
        var artwork = $('#artwork').val();
        var rarity = $('#rarity').val();
        var cost = $('#cost').val();
        var type = $('#type').val();
        var text = $('#text').val();
        var card = {};
        card.name = name;
        card.layout = layout;
        card.artwork = artwork;
        card.rarity = rarity;
        card.cost = cost;
        card.text = text;
        $.post('writecard.php', card, function(data){
            console.log(data);
        });
    </script>
</html>

输出:

name: Transfer Power -- artwork: http://static.tvtropes.org/pmwiki/pub/images/Voyager_shield.jpg -- rarity: common -- cost: 3 -- text: Until the start of your next turn, target friendly Ship gains Shield +X and Damage -X where X is the Ship's current Damage. --

为了把javascript从等式中去掉——如果它是一个简单的形式呢?

<html>
  <body>
      <form action="writecard.php">
        <input id="name" name="name" value="Transfer Power" />
        <br/>
        <input id="layout" name="layout"  value="cic" />
        <br/>
        <input id="artwork" name="artwork"  value="http://static.tvtropes.org/pmwiki/pub/images/Voyager_shield.jpg" />
        <br/>
        <input id="rarity" name="rarity"  value="common" />
        <br/>
        <input id="cost" name="cost"  value="3" />
        <br/>
        <input id="type" name="type"  value="unknown" />
        <br/>
        <input id="text" value="Until the start of your next turn, target friendly Ship gains Shield +X and Damage -X where X is the Ship's current Damage." />
        <br/>
        <input type="submit" />
      </form>
  </body>
</html>