คำขอแพทช์ AngularJs

ฉันกำลังพยายามอัปเดตข้อมูลผ่าน API ของฉันโดยใช้วิธี http Patch แต่ฉันได้รับการตอบสนองที่ไม่ถูกต้องหรือข้อผิดพลาดเซิร์ฟเวอร์ภายใน

นี่คือการโทร JSON ของฉัน:

$http.patch(baseUrl + '/users/' + currentUserEmail,data).success(success).error(error)

person Aslam    schedule 28.10.2015    source แหล่งที่มา
comment
วิธี API PATCH ของฉันต้องการส่วนหัว IF-Match แต่ไม่ได้เพิ่มโดยอัตโนมัติหรือไม่   -  person Aslam    schedule 28.10.2015


คำตอบ (1)


คุณสามารถเพิ่มส่วนหัวที่จำเป็นได้โดยใช้พารามิเตอร์ตัวเลือกที่ 3 ของ $http.patch :

var config = {headers: {'IF-Match': 'your-data'}};
$http.patch(baseUrl + '/users/' + currentUserEmail,data, config).success(success).error(error)

เอกสารประกอบให้ข้อมูลเกี่ยวกับตัวเลือกการกำหนดค่าที่กำหนดเอง

หากคุณต้องการเพิ่มส่วนหัวที่กำหนดเองให้กับทุกคำขอโดยอัตโนมัติ คุณสามารถใช้ https://docs.angularjs.org/api/ng/service/$http#interceptors :

angular.module('app').factory('HttpInterceptor', function () {
  return {
    request: function (config) {
      if (config.method === 'PATCH')
         config.headers['IF-Match'] = 'your-data';
      return config;
    }
  };
});

angular.module('app').config(['$httpProvider', '$resourceProvider', function ($httpProvider, $resourceProvider) {
    // Add the interceptor to the $httpProvider to intercept http calls
    $httpProvider.interceptors.push('HttpInterceptor');
}])

แก้ไข: เพื่อตอบความคิดเห็นของคุณเกี่ยวกับวิธีรับข้อมูลจากคำขอ GET ใน http interceptor คุณสามารถสกัดกั้นการตอบสนองได้เช่นกัน:

angular.module('app').factory('HttpInterceptor', function () {
 var etag = null;
  return {
    request: function (config) {
      if (config.method === 'PATCH')
         config.headers['IF-Match'] = etag;
      return config;
    },
   response: function (response) {
      if (response.config.method === 'GET')
          etag = reponse.config.headers['e-tag'];
        // Return the response or promise.
      return response || $q.when(response);
    },
  };
});
person Alexandre Nucera    schedule 28.10.2015
comment
วิธีรับข้อมูล If-Match เนื่องจากควรเท่ากับค่า E-Tag จาก Get Response ฉันจะอ่านค่านั้นได้อย่างไร - person Aslam; 28.10.2015
comment
พิมพ์วัตถุ config ใน http interceptor ซึ่งมีทุกสิ่งที่คุณต้องการ - person Alexandre Nucera; 28.10.2015
comment
ฉันสามารถเพิ่มค่า If-Match ให้กับคำขอได้ แต่ยังไม่ชัดเจนเกี่ยวกับวิธีการรับค่าจากการตอบกลับของวิธีการ get - person Aslam; 28.10.2015
comment
ฉันแก้ไขคำตอบของฉัน (ด้วยรหัสหลอก ฉันยังไม่ได้ทดสอบ) - person Alexandre Nucera; 28.10.2015
comment
ใช่ ฉันได้เปลี่ยนการกำหนดค่าเป็นการตอบสนองในฟังก์ชันตอบสนอง แต่ทั้งหมดที่ฉันได้รับคือ NULL ฉันคิดว่าฟังก์ชันตอบสนองได้รับการตอบสนองจากเซิร์ฟเวอร์อย่างเหมาะสม มีความคิดอะไรบ้าง? - person Aslam; 28.10.2015
comment
โมฆะคืออะไร? ออบเจ็กต์การตอบสนองมีออบเจ็กต์การกำหนดค่าเพื่อให้คุณสามารถทำ response.config.headers ได้ - person Alexandre Nucera; 28.10.2015
comment
ให้เราสนทนาต่อในการแชท - person Aslam; 28.10.2015
comment
มีปัญหากับวิธีแก้ปัญหานี้ etag ของทุกคำขอจะถูกบันทึกไว้ในนั้น เช่น. มี etag สำหรับรับไฟล์ css, ไฟล์ html ฯลฯ ดังนั้นฉันจึงต้องการรับ etag เมื่อผู้ใช้ใช้วิธี GET เพื่อรับข้อมูลของผู้ใช้จาก API - person Aslam; 01.11.2015
comment
วัตถุ config มีคุณสมบัติอื่น ๆ เช่น url ดังนั้นให้แก้ไขเงื่อนไข if ในฟังก์ชันคำขอเพื่อให้ตรงกับความต้องการของคุณ (เช่น: บันทึก etag เฉพาะเมื่อคุณเข้าถึง URL ที่ระบุเท่านั้น) - person Alexandre Nucera; 07.11.2015