doujia9204 2014-06-01 10:12
浏览 54
已采纳

extjs 4.2在编辑时创建“post”请求而不是“put”

I am working on extjs 4.2 app with php mysql backend. I am able to add records to store via form. Than store autosyncs using proxies add new record to mysql. However, it doesnt create a "put" request and instead create a 'post' request. I have seen other samples regarding the same issue and most of them points towards configuring model's idProperty.

My frontend uses a form to create new/POST or to edit existing/PUT depending on presence of unique model ID

store uses ajax proxy

        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 file:

        <?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);
}

?>
  • 写回答

1条回答 默认 最新

  • doucuyu2259 2014-06-01 12:57
    关注

    If you're going to stay with the Ajax proxy, you can adjust the actionMethods property to specify which HTTP verbs should be used for create, update, etc.

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

    Alternatively (and probably better), you could use the Rest proxy to let Ext JS assign the verbs and generate the appropriate URLs automatically.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码