php - extjs 4.2 在编辑时创建 "post"请求而不是 "put"

标签 php mysql extjs extjs4

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

我的前端使用表单根据唯一模型 ID 的存在来创建新的/POST 或编辑现有的/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);
}

?>

最佳答案

如果您打算继续使用 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

关于php - extjs 4.2 在编辑时创建 "post"请求而不是 "put",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23978753/

相关文章:

php - 使用Elasticsearch 5.0创建索引会引发Bad Request异常

mysql - 如何从MySQL获取特定标签的文章

javascript - 我如何在 ExtJS 中重用组件?

javascript - 如何将远程数据加载到网格中?

MySQL - 按空值分组

android - sencha 包无法在 Release模式下运行 zipalign

php - 如何使用 PHP 添加保持登录状态?

php - 如何使用 php 迭代整个 mysql 表

php - 使用 PHP 进行 MySQL 查询时不显示结果

mysql - 选择每首歌曲中观看次数最多的视频