Javascript 使用Jquery更新函数REST

Javascript 使用Jquery更新函数REST,javascript,jquery,ajax,rest,Javascript,Jquery,Ajax,Rest,学习了很多关于如何使用REST的知识,目前我能够创建和删除项目,但无法使更新功能正常工作。我尝试了这么多组合,但不断收到400个错误请求,指出data.d.\uu metadata.etag无效 这是导致故障的代码: // Saves a incident. If there is no value in the #Incident_ID hidden field then // a new incident is created by "POST" request. Otherwise an

学习了很多关于如何使用REST的知识,目前我能够创建和删除项目,但无法使更新功能正常工作。我尝试了这么多组合,但不断收到400个错误请求,指出
data.d.\uu metadata.etag
无效

这是导致故障的代码:

// Saves a incident. If there is no value in the #Incident_ID hidden field then
// a new incident is created by "POST" request. Otherwise an existing incident
// is updated with "PUT" request.
saveIncident: function (data) {
    var requestType = $('#Incident_ID').val() != '' ? 'PUT' : 'POST';
    var CREATE_Headers = {"accept": "application/json;odata=verbose"};
    var UPDATE_Headers = {"accept" : "application/json;odata=verbose","X-HTTP-Method":"MERGE","If-Match" : "data.d.__metadata.etag"};
    var headertype = $('#Incident_ID').val() != '' ? UPDATE_Headers : CREATE_Headers;

    $.ajax({
        url: data.d.__metadata.etag,
        type: requestType,
        processData: false,
        contentType: "application/json;odata=verbose",
        headers: headertype,
        data: JSON.stringify(IncidentManager.collectFieldValues()),
        success: function (data) {
        console.log("These are the resulsts "+data.d);
        }
    });
},
我手动输入URL,但仍然遇到同样的问题。我将IF-Match头更改为
W/“3”
,然后开始出现错误,说
3
不是有效的头值。我将标题更改为
“If Match”:“data.d.\uu metadata.type”
,然后我开始收到错误,说
d
不是有效的标题值。我不太清楚我在这里遗漏了什么

我从以下几个例子中找到了自己的基础:

我尝试了几种不同的组合,但都没能成功。下面是我的javascript代码。如果您有任何建议,我将不胜感激,因为我已经没有头发了

var IncidentManager = {
    // Returns the url of the application server of a demo web app.
    basePath: function () { return '../../../../_vti_bin/listData.svc'; },

    // Formats a URI of a product
    createIncidentUrl: function (requestType) {
        if (requestType == 'POST')
            return this.basePath() + '/GDI_PROD_Incidents';
        return this.basePath() + '/GDI_PROD_Incidents('+ encodeURIComponent($('#Incident_ID').val())+')';
    },  
    // Creates an object with the properties retrievd from the input fields 
    // of the "ProductDetails" form.
    collectFieldValues: function () {
        return {
            Description: $('#Description').val(),
            Incident: $('#Incident').val(),
            ÉtatValue: $('#état').val(),
            PrioritéValue: $('#Priorité').val(),
            Duré: $('#Duré').val(),
            Date_de_début:  $('#DateDeDébut').val(),
            Date_de_fin:  $('#DateDeFin').val(),
            Autres_Groupe_Support_Prime: $('#Autres_Groupe_Support_Prime').val(),
            ResponsableValue: $('#Prime').val(),
            Impact: $('#Impact').val(),
            Temps_Consacré: $('#Temps_Consacré').val(),
            Type_de_tempsValue: $('#Type_de_temps').val(),
            Journal_des_actions: $('#Journal_des_actions').val(),
            Dépanage: $('#Dépannage_effectué').val(),
            Suivi: $('#Suivi').val(),
            Ressources: $('#Ressources').val()
        };
    },
    // Saves a incident. If there is no value in the #Incident_ID hidden field then
    // a new incident is created by "POST" request. Otherwise an existing incident
    // is updated with "PUT" request.
    saveIncident: function (data) {
        var requestType = $('#Incident_ID').val() != '' ? 'PUT' : 'POST';
        var CREATE_Headers = {"accept": "application/json;odata=verbose"};
        var UPDATE_Headers = {"accept" : "application/json;odata=verbose","X-HTTP-Method":"MERGE","If-Match" : "data.d.__metadata.etag"};
        var headertype = $('#Incident_ID').val() != '' ? UPDATE_Headers : CREATE_Headers;

        $.ajax({
            url: data.d.__metadata.etag,
            type: requestType,
            processData: false,
            contentType: "application/json;odata=verbose",
            headers: headertype,
            data: JSON.stringify(IncidentManager.collectFieldValues()),
            success: function (data) {
            console.log("These are the resulsts "+data.d);
            }
        });
    },
};

明白了!经过大量的研究,我发现了两个博客,它们解释了很多关于ETAG的内容,并使我更清楚地了解了如何使用我的if-match语句

最后,我使用了特殊值“*”,以便允许任何值匹配。因此,我将代码更改为:

var UPDATE_Headers = {"accept" : "application/json;odata=verbose","X-HTTP-Method":"MERGE","If-Match" : "data.d.__metadata.etag"};
为此:

var UPDATE_Headers = {"accept" : "application/json;odata=verbose","X-HTTP-Method":"MERGE","If-Match" : "*"};
接下来,我又开始使用我的头,特别是“X-HTTP-Method”:“MERGE”出现错误。错误表明我不能将X-HTTP-Method与PUT一起使用,必须以POST的形式发送。在此处找到另一个博客,详细解释如何正确使用它:

现在,以下是适用于我的更新代码:

var IncidentManager = {
    // Returns the url of the application server of a demo web app.
    basePath: function () { return '../../../../_vti_bin/listData.svc'; },

    // Creates an object with the properties retrievd from the input fields 
    // of the "ProductDetails" form.
    collectFieldValues: function () {
        return {
            Description: $('#Description').val(),
            Incident: $('#Incident').val(),
            ÉtatValue: $('#état').val(),
            PrioritéValue: $('#Priorité').val(),
            Duré: $('#Duré').val(),
            Date_de_début:  $('#DateDeDébut').val(),
            Date_de_fin:  $('#DateDeFin').val(),
            Autres_Groupe_Support_Prime: $('#Autres_Groupe_Support_Prime').val(),
            ResponsableValue: $('#Prime').val(),
            Impact: $('#Impact').val(),
            Temps_Consacré: $('#Temps_Consacré').val(),
            Type_de_tempsValue: $('#Type_de_temps').val(),
            Journal_des_actions: $('#Journal_des_actions').val(),
            Dépanage: $('#Dépannage_effectué').val(),
            Suivi: $('#Suivi').val(),
            Ressources: $('#Ressources').val()
        };
    },
    // Saves a incident. If there is no value in the #Incident_ID hidden field then
    // a new incident is created by "POST" request. Otherwise an existing incident
    // is updated with "PUT" request.
    saveIncident: function (data) {

        var New_Incident_URL = this.basePath() + '/GDI_PROD_Incidents';
        var UPDATE_Incident_URL = this.basePath() + '/GDI_PROD_Incidents('+ encodeURIComponent($('#Incident_ID').val())+')';
        var createIncidentUrl = $('#Incident_ID').val() != '' ? UPDATE_Incident_URL : New_Incident_URL;

        var CREATE_Headers = {"accept": "application/json;odata=verbose"};
        var UPDATE_Headers = {"accept" : "application/json;odata=verbose","X-HTTP-Method":"MERGE","If-Match":"*"};
        var headertype = $('#Incident_ID').val() != '' ? UPDATE_Headers : CREATE_Headers;

        $.ajax({
            url: createIncidentUrl,
            type: 'POST',
            processData: false,
            contentType: "application/json;odata=verbose",
            headers: headertype,
            data: JSON.stringify(IncidentManager.collectFieldValues()),
            success: function (data) {
            console.log(data);
            }
        });
    },
};

我认为它应该是
“If Match”:data.d._metadata.etag
,不带引号,这给了我
未捕获的类型错误:无法读取未定义的属性“d”:(这意味着数据没有
d
作为已知属性。您可以调试并检查
data
有哪些属性。调试此属性的最佳方法是什么?
console.log(data)
并检查浏览器控制台窗口。
- A PUT request is used to update an entire entity. If no values are specified for fields in the entity, the fields will be set to default values.

- A MERGE request is used to update only those field values that have changed. Any fields that are not specified by the operation will remain set to their current value.
var IncidentManager = {
    // Returns the url of the application server of a demo web app.
    basePath: function () { return '../../../../_vti_bin/listData.svc'; },

    // Creates an object with the properties retrievd from the input fields 
    // of the "ProductDetails" form.
    collectFieldValues: function () {
        return {
            Description: $('#Description').val(),
            Incident: $('#Incident').val(),
            ÉtatValue: $('#état').val(),
            PrioritéValue: $('#Priorité').val(),
            Duré: $('#Duré').val(),
            Date_de_début:  $('#DateDeDébut').val(),
            Date_de_fin:  $('#DateDeFin').val(),
            Autres_Groupe_Support_Prime: $('#Autres_Groupe_Support_Prime').val(),
            ResponsableValue: $('#Prime').val(),
            Impact: $('#Impact').val(),
            Temps_Consacré: $('#Temps_Consacré').val(),
            Type_de_tempsValue: $('#Type_de_temps').val(),
            Journal_des_actions: $('#Journal_des_actions').val(),
            Dépanage: $('#Dépannage_effectué').val(),
            Suivi: $('#Suivi').val(),
            Ressources: $('#Ressources').val()
        };
    },
    // Saves a incident. If there is no value in the #Incident_ID hidden field then
    // a new incident is created by "POST" request. Otherwise an existing incident
    // is updated with "PUT" request.
    saveIncident: function (data) {

        var New_Incident_URL = this.basePath() + '/GDI_PROD_Incidents';
        var UPDATE_Incident_URL = this.basePath() + '/GDI_PROD_Incidents('+ encodeURIComponent($('#Incident_ID').val())+')';
        var createIncidentUrl = $('#Incident_ID').val() != '' ? UPDATE_Incident_URL : New_Incident_URL;

        var CREATE_Headers = {"accept": "application/json;odata=verbose"};
        var UPDATE_Headers = {"accept" : "application/json;odata=verbose","X-HTTP-Method":"MERGE","If-Match":"*"};
        var headertype = $('#Incident_ID').val() != '' ? UPDATE_Headers : CREATE_Headers;

        $.ajax({
            url: createIncidentUrl,
            type: 'POST',
            processData: false,
            contentType: "application/json;odata=verbose",
            headers: headertype,
            data: JSON.stringify(IncidentManager.collectFieldValues()),
            success: function (data) {
            console.log(data);
            }
        });
    },
};