我想用PHP中的AngularJS和Arrest REST API做一个简单的CRUD应用程序作为后端。
GET和DELETE工作正常。但PUT和POST给我带来了麻烦。我需要提供的数据不是JSON,而是采用更基本的格式(我不想过多地研究Arrest API)。
我尝试使用jQuery完成一个简单的测试,让PUT和POST工作,我得到了以下网络活动。这里显示的POST工作正常。
`Request URL:http://anyhost.com:8888/angular18/api/allIP
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:43
Content-Type:application/x-www-form-urlencoded
Host:anyhost.com:8888
Origin:http://anyhost.com:8888
Referer:http://anyhost.com:8888/angular18/api/api-test.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
name:mynewxxx
provider:xxx
technology:28xxx
Response Headersview source
Connection:Keep-Alive
Content-Length:44
Content-Type:application/json
Date:Fri, 19 Jul 2013 19:45:46 GMT
Keep-Alive:timeout=5, max=99
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`
现在,当我使用AngularJS作为工厂时使用以下代码
TodoApp.factory('Todo', function($resource) {
return $resource('api/allIP/:id', {id:'@id'}, {update:{method: 'PUT'}});
});
我得到了如下的POST网络活动,实际插入数据库不起作用。 我想我应该能够强制AngularJS以不同的方式编写有效负载,但我不知道如何做到这一点。 如果有人可以帮助我会很棒 谢谢 彼得
BTW:我在这个问题条目中将所有localhost信息更改为anyhost.com,因为它不允许我保留localhost
`Request URL:http://anyhost.com:8888/angular18/api/allIP
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:50
Content-Type:application/json;charset=UTF-8
Host:anyhost.com:8888
Origin:http://anyhost.com:8888
Referer:http://anyhost.com:8888/angular18/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview parsed
{"name":"xxx","provider":"xxx","technology":"xxx"}
Response Headersview source
Connection:Keep-Alive
Content-Length:45
Content-Type:application/json
Date:Sat, 20 Jul 2013 07:09:29 GMT
Keep-Alive:timeout=5, max=100
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.
`
答案 0 :(得分:0)