【问题标题】:extjs 4.2 creating "post" request while editing instead of "put"extjs 4.2在编辑而不是“put”时创建“post”请求
【发布时间】:2014-06-01 20:57:53
【问题描述】:

我正在开发带有 php mysql 后端的 extjs 4.2 应用程序。我可以通过表单将记录添加到存储中。比使用代理存储自动同步向 mysql 添加新记录。 但是,它不会创建“放置”请求,而是创建“发布”请求。我看过其他关于同一问题的示例,其中大多数指向配置模型的 idProperty。

我的前端使用表单来创建新/POST 或根据唯一模型 ID 的存在来编辑现有/PUT

商店使用 ajax 代理

        Save controller:
var form = this.getAnnDetailsPanel1().getForm();
var formData = form.getFieldValues();
//console.log(formData);

var a = form.findField('annID');
var b = a.getValue();
//console.log(b);
if(!b)
{
//console.log("record doesnt exist - Add Record");
var store = Ext.data.StoreManager.get('announcementStore');
var sss = store.add(formData);
}
else
{
console.log("record exist - Edit Record");
var ttt = form.getRecord();

uuu = form.updateRecord(ttt);
ttt.commit();
console.log(ttt);

}

php 文件:

        <?php
header('Content-Type: application/json');

$method = $_SERVER["REQUEST_METHOD"];
$con = new PDO("mysql:host=localhost;dbname=openclass", "root", "")  or die("cannot connect to mysql");

switch ($method) {
    case "GET":     // Return all records
        $ClassID = $_GET["ClassID"];
        $sql =  "SELECT * FROM announcement " .
                "WHERE ClassID = $ClassID " .
                "ORDER BY annID";

        $sql = $con->prepare($sql);
        $sql->execute();
        $rows = array();

        while ($row = $sql->fetch(PDO::FETCH_OBJ)) {
            $rows['data'][] = $row;
        }

        echo json_encode($rows, JSON_NUMERIC_CHECK);

        break;

    case "PUT":     // Update existing record, requires ID
        $postData = getPostData();
        $annID = getPostValue($postData, 'annID');
        $ClassID = getPostValue($postData, 'ClassID');
        $annHeading = getPostValue($postData, 'annHeading');
        $annContent = getPostValue($postData, 'annContent');

        $sql =  "UPDATE announcement " .
                "SET ClassID = '$ClassID' " .
                " annHeading = '$annHeading' " .
                " annContent = '$annContent' " .
                "WHERE annID = '$annID' ";

        $sql = $con->prepare($sql);
        $result = $sql->execute();

        break;

    case "POST":    // New record
        $postData = getPostData();
        $ClassID = getPostValue($postData, 'ClassID');
        $annHeading = getPostValue($postData, 'annHeading');
        $annContent = getPostValue($postData, 'annContent');

        $sql =  "INSERT INTO announcement (ClassID, annHeading, annContent) " .
                "VALUES ('$ClassID','$annHeading', '$annContent')";

        $sql = $con->prepare($sql);
        $result = $sql->execute();



        break;

    case "DELETE":  // Delete existing record, requires annID
        $postData = getPostData();
        $annID = getPostValue($postData, 'annID');

        $sql =  "DELETE FROM announcement " .
                "WHERE annID = '$annID' ";

        $sql = $con->prepare($sql);
        $result = $sql->execute();

        if (! $result) {
            echo "{\"success\": false}";
        } else {
            echo "{\"annID\": $annID}";
        }

        break;
}

$con = null;

function getPostData() {
    $fileContents = file_get_contents("php://input");
    return json_decode($fileContents, true);
}

function getPostValue($postData, $fieldName) {
    return (!empty($postData[$fieldName]) ? htmlspecialchars($postData[$fieldName]) : NULL);
}

?>

【问题讨论】:

    标签: php mysql extjs extjs4


    【解决方案1】:

    如果您打算继续使用 Ajax 代理,您可以调整 actionMethods 属性以指定应该使用哪些 HTTP 动词来创建、更新等。

    http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.proxy.Ajax-property-actionMethods

    或者(可能更好),您可以使用 Rest 代理让 Ext JS 分配动词并自动生成适当的 URL。

    http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.proxy.Rest

    【讨论】:

    • 您好,请解散。谢谢回复。我已经对存储进行了更改。现在我可以发送“放置”请求。但是,编辑尚未完成。顺便说一句,你已经在你的网站上发布了不错的 extjs 应用系列
    • 嗨,我将商店从 ajax 更改为 rest 并且它有效。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多