0

I am trying to update a JSON object (e.g: customer). But, I have the following error:

"NetworkError: 405 Method Not Allowed - http://prestashop/api/..."

This is my code (index.js):

var testWebService = angular.module('testWebService', []);

testWebService.controller('testWebServiceCtrl', function ($scope, $http) {

    var $baseDir = "http://prestashop/api";
    var $objectDir = "customers";
    var $idObjectDir = "1";
    var $keyDir = "F517VWPRREG7TA25DEY8UIZT8V79E5OV";
    var $urlDir = $baseDir + "/" + $objectDir + "/" + $idObjectDir + "?ws_key=" + $keyDir + "&output_format=JSON";

    // TEST DE LA METHODE PUT SUR L ID D UN CUSTOMER
    $.ajax({
        type: "PUT",
        url: $urlDir,
        dataType: "json",
        async: false,
        contentType: "application/json; charset=utf-8",
        data: {"id": "93"},
        crossDomain: true,
        success: function () { 
            console.log("Ok PUT");
        },
        error: function() {
            console.log("Erreur PUT");
        }
    });
});

Before, I tried to GET the id of an object (same: customer) and I succeeded with an almost similar method. I precise that I gave rights about "customer" in Advanced Settings / webservice (for method GET, PUT, POST ...).

Thanks in advance for your help, I tried so many things, but whitout success.

PS: If you have any suggestion about my code to "clean" it, you were pleased.

My webservice JSON:

{"customer":{"id":1,"id_default_group":"3","id_lang":"1","newsletter_date_add":"2013-12-13 08:19:15","ip_registration_newsletter":"","last_passwd_gen":"2015-06-08 03:38:27","secure_key":"7036cdf99ea12125ad1b3789f298f686","deleted":"0","passwd":"2e372235eb5213bc004ce72bcfef16a2","lastname":"DOE","firstname":"John","email":"pub@prestashop.com","id_gender":"1","birthday":"1970-01-15","newsletter":"1","optin":"1","website":"","company":"","siret":"","ape":"","outstanding_allow_amount":"0.000000","show_public_prices":"0","id_risk":"0","max_payment_days":"0","active":"1","note":"","is_guest":"0","id_shop":"1","id_shop_group":"1","date_add":"2015-06-08 09:38:27","date_upd":"2015-06-08 09:38:27","associations":{"groups":[{"id":"3"}]}}}

EDIT: When I tried with the method GET, I did:

$.ajax({
    type: "GET",
    url: $urlDir,
    dataType: "json",
    async: false,
    success: function (data) { 
        $scope.customer1 = data;
        console.log("Ok GET");
        console.log(data);
    },
    error: function() {
        console.log("Erreur GET");
    }
});
Stalyon
  • 538
  • 5
  • 20

1 Answers1

0

Reading the status code definition of the http protocol, try adding the following property to the ajax request:

$.ajax({
    type: "PUT",
    ...
    beforeSend: function (xhr) {
        xhr.setRequestHeader("Allow", "GET, HEAD, PUT, DELETE");
    },
    ...
});

PS: If you have CORS disabled on your server, look this answer and set the headers to allow your server access to requests from different origins.

Community
  • 1
  • 1
  • I added it: same results. – Stalyon Jun 16 '15 at 13:53
  • I have never tried prestashop api, but check this link, maybe it helps you: http://doc.prestashop.com/display/PS15/Chapter+2+-+Discovery+-+Testing+access+to+the+web+service+with+the+browser You need to retrieve (GET) the customer first and then update that object. I don't know if you are getting the customer properly – andresigualada Jun 16 '15 at 14:09
  • I just edited my code to show you how I am getting the customer. – Stalyon Jun 16 '15 at 14:13
  • The problem with the PrestaShop doc is they use XML and PHP. I want to do that with JSON and JS/Ajax/AngularJS – Stalyon Jun 16 '15 at 14:14
  • Did the GET method works? I mean, can you retrieve properly the customer or the api is giving you an error? – andresigualada Jun 16 '15 at 14:16
  • I suggest you to check the request headers with firebug or another debug tool to see if the request is sending correctly. This way you can check if there is a bug in your client-side – andresigualada Jun 16 '15 at 14:22
  • The error I have with Firebug is: "NetworkError: 405 Method Not Allowed". – Stalyon Jun 16 '15 at 14:42
  • "NetworkError: 405 Method Not Allowed - http://..." Erreur PUT index.js (ligne 43) Blocage d'une requête multi-origines (Cross-Origin Request) : la politique « Same Origin » ne permet pas de consulter la ressource distante située sur http://... (Raison : échec du canal de pré-vérification des requêtes CORS. Blocage d'une requête multi-origines (Cross-Origin Request) : la politique « Same Origin » ne permet pas de consulter la ressource distante située sur http://... Raison : échec de la requête CORS. – Stalyon Jun 16 '15 at 14:47
  • You have to enable CORS in your server-side. Look [this answer](http://stackoverflow.com/a/20423411/2026167) and set the headers to allow your server access to requests from different origins. – andresigualada Jun 16 '15 at 14:53
  • I have ever done that before (for the GET method especially). – Stalyon Jun 16 '15 at 14:59
  • I suggest you to check all the process from the beginning. Your server is not accepting your request for some reason, and it says it's because CORS is not properly set up. Debug this first and then go for the PUT method. – andresigualada Jun 16 '15 at 15:04
  • Yes, this is the way I am trying to follow. Thanks for your help andresigualada. – Stalyon Jun 16 '15 at 15:05
  • Hey @Stalyon any news? I need it too – icy Jun 29 '15 at 16:20
  • @icy2k : I didn't understand why, but, when I do that, it works : in WebserviceRequest.php, I put the following code : die($this->method); (line 861). Then, I try again. Next, I comment this line and it works ... Very incomprehensible ... – Stalyon Jun 30 '15 at 12:58