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

时间:2013-11-26 17:41:29

标签: javascript php jquery json post

我正在制作卡片生成器以获得乐趣并学习如何从输入中收集数据并写入数据库。

尝试发送这段简单的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仅在我删除网址的方案时才有效。我希望这很简单。这是代码 -

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 . ' -- ';
    }
?>

再次:它成功发送POST并在删除URL方案时返回数据。

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

感谢阅读。 :

1 个答案:

答案 0 :(得分:0)

根据您的示例使用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>