gilbitron / Arrest-MySQL

A "plug-n-play" RESTful API for your MySQL database.
131 stars 219 forks source link

AngularJS connecting to Arrest-MySQL not working for PUT and POST #4

Open roestigraben opened 11 years ago

roestigraben commented 11 years ago

Hello,

Iam learning AngularJS and started to do an application (really more following a tutorial) that implements CRUD services. So, I have seen Arrest-MySQL and was happy to see a simple yet powerful REST API easy to use. The GET methods were uo and running quite quickly, as well as the DELETE operations. Then problems started with PUT. As PUT did not work, I tried to try with POST first, but here I got into problems.

Maybe somebody can give the fight hints on how to proceed. (I must say that I am not at all an expert in php)

In order to understand better, I did try to interface with the API with the help of a REST Console (Actually I tried with REST Console 4.0.2 and Simple Rest Console) Funnily, PUT was working great, but POST did not on both :-(

I past hereunder the network activity on PUT and POST. Several difference are notable The RequestURL, the PUT, the request payload are differnet to a certain extent.

MAny thanks for any help Peter

PUT coming from my AngularJS App. There is a response saying code 200: success, but the database is not updated.

Request URL:http://localhost:8888/angular18/api/allIP/16name=USB2PHY1port&provider=TR%26D Request Method:PUT Status Code:200 OK Request Headersview parsed PUT /angular18/api/allIP/16?name=USB2PHY1port&provider=TR%26D HTTP/1.1 Host: localhost:8888 Connection: keep-alive Content-Length: 789 Accept: application/json, text/plain, / Origin: http://localhost:8888 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36 Content-Type: application/json;charset=UTF-8 Referer: http://localhost:8888/angular18/ Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Cookie: SQLiteManager_currentLangue=2 Query String Parametersview parsed name=USB2PHY1port&provider=TR%26D Request Payloadview parsed {"id":"16","name":"USB2PHY1port","provider":"TR&D","technology":"40LP","cost":"250","description":"specifically done for STE project AVU9600. 1V8 GO2 compatible","type":"USB2.0 PHY","maturity_status":"MAT5","status_date":"Q1/11","next_Maturity":"MAT10","next_Date":"Q3/11","startMAT10":"Q1/11","completeMAT10":"Q3/11","completeMAT20":"Q2/12","HED":"n","HED_criticality":"","HED_MAT20_request":"Q1/11","CCI":"y","CCI_criticality":"","CCI_MAT20_request":"Q1/11","STE":"y","STE_criticality":"2","STE_MAT20_request":"Q2/11","APG":"n","APG_criticality":"3","APG_MAT20_request":"Q1/11","IMS":"n","IMS_criticality":"1","IMS_MAT20_request":"Q1/11","contactName1":"Adrian","contactName2":"","reserved1":"23345","reserved2":"334-88","reserved3":"Tue Feb 21 21:41:50 GMT+0100 2012","reserved4":"LYS"} Response Headersview parsed HTTP/1.1 200 OK Date: Sat, 29 Jun 2013 06:56:28 GMT Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8x DAV/2 PHP/5.3.6 X-Powered-By: PHP/5.3.6 Content-Length: 44 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: application/json

Here is the PUT method (albeit not on the same id in the database) done with the console and which is successful

Request URL:http://localhost:8888/angular18/api/allIP/18 Request Method:PUT Status Code:200 OK Request Headersview parsed PUT /angular18/api/allIP/18 HTTP/1.1 Host: localhost:8888 Connection: keep-alive Content-Length: 46 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_68) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36 Origin: chrome-extension://fhjcajmcbmldlhcimfajhfbgofnpcjmb Content-Type: application/xml Accept: /_ Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Cookie: SQLiteManager_currentLangue=2 Request Payload name=hansnew&provider=synopsys&technology=28lp Response Headersview parsed HTTP/1.1 200 OK Date: Sat, 29 Jun 2013 07:18:03 GMT Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8x DAV/2 PHP/5.3.6 X-Powered-By: PHP/5.3.6 Content-Length: 44 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: application/json

nmstoker commented 10 years ago

Maybe you've abandoned this (it's been a while!) but as I'm facing similar issues, thought I'd mention one point: I believe it's useful to look at the actual JSON response returned, not just the HTTP response (which is 200 in your case above).

I get a 200, but looking at the content of the response, I get what's shown in the README.md, as:

{ "error": { "message": "No Content", "code": 204 } }

I'm no PHP guru myself, but am trying to figure out what's going wrong... no break-through so far though!

nmstoker commented 10 years ago

Sorry - just saw the discussion at the end of #1 where you seem to have found the cause of the problem

roestigraben commented 10 years ago

Hi Neil,

it¹s been a while indeed. Many thanks to try to help

I found my way around in between. I could explain what I did if you are interested . Just let me know and give me a little time to respond as i am very busy currently (say over the weekend)

BR Peter

From: Neil Stoker notifications@github.com Reply-To: gilbitron/Arrest-MySQL <reply+i-16172179-abc8ac87bb60f7c5236db1169bc9986a2716f1cd-4879307@reply.git hub.com> Date: Monday 28 April 2014 19:05 To: gilbitron/Arrest-MySQL Arrest-MySQL@noreply.github.com Cc: roestigraben peter.hirt@wanadoo.fr Subject: Re: [Arrest-MySQL] AngularJS connecting to Arrest-MySQL not working for PUT and POST (#4)

been

roestigraben commented 10 years ago

As said, don¹t hesitate to ask how I did it

From: Neil Stoker notifications@github.com Reply-To: gilbitron/Arrest-MySQL <reply+i-16172179-abc8ac87bb60f7c5236db1169bc9986a2716f1cd-4879307@reply.git hub.com> Date: Monday 28 April 2014 19:08 To: gilbitron/Arrest-MySQL Arrest-MySQL@noreply.github.com Cc: roestigraben peter.hirt@wanadoo.fr Subject: Re: [Arrest-MySQL] AngularJS connecting to Arrest-MySQL not working for PUT and POST (#4)

Sorry - just saw the discussion at the end of #1 https://github.com/gilbitron/Arrest-MySQL/issues/1 where you seem to have found the cause of the problem

‹ Reply to this email directly or view it on GitHub https://github.com/gilbitron/Arrest-MySQL/issues/4#issuecomment-41585130 .

nmstoker commented 10 years ago

Hi Peter,

If you could give details that would be really helpful - of course, only when it suits and things are less busy for you!

Many thanks, Neil

roestigraben commented 10 years ago

PUT and POST are different from GET and DELETE as they move actual data from the application to the database. AngularJS and jQuery Ajax are different in that AngularJS uses JSON dataformat while jQuery uses like the concatenation of the key=value pairs as id=33&title=myTitle&.......

For those two methods I finally use the $http service rather than $resource.

I also force the headers to be specifically like below headers: {'Content-Type': 'application/x-www-form-urlencoded'}

the code to add a new record is then

var CreateCtrl = function ($scope, $location, $http, Todo) { $scope.save = function(){ // use a variable dataConstruct to construct the payload var dataConstruct = ('name=' + $scope.item.name + '&provider=' + $scope.item.provider + '&technology=' + $scope.item.technology); //console.log(dataConstruct); $http({method: 'POST', url: 'api/allIP', data: dataConstruct, headers: {'Content-Type': 'application/x-www-form-urlencoded'} }). success(function(data, status) { $scope.status = status; $scope.data = data; $location.path('/'); }). error(function(data, status) { $scope.data = data || "Request failed"; $scope.status = status; }); }; };

I hope this brings you one step forward

nmstoker commented 10 years ago

Hi Peter, This is very helpful - thank you for sharing the details! Kind regards, Neil