AngularJS post 方法无效
AngularJS post method is not working
我正在尝试将一些数据与 post 一起发送到特定的 url,后者背后有一个 php 脚本。目前我无法访问 php 脚本。 php 脚本检查字符串是否与数据库中的任何记录匹配,如果匹配则 return 记录。如果没有匹配,脚本将 return 所有记录。
以下代码是我目前所拥有的。如您所见,我有一个字符串
命名:不应该找到任何结果字符串。这实际上应该不会 return 任何结果。但是它 return 是所有记录而不是没有记录。
我尝试了什么:
- 使用
params
代替data
- 使用不同的
Content-types
- 使用 post 方法的简短版本
$http({
url: $scope.url,
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {search: "shouldnotfindanyresultsstring"}
}).then(function (response) {
console.log(response);
}, function (response) { // optional
console.log("Still not working");
});
所以最终我想用搜索字符串在数据库中搜索记录。但是我没有让它工作。
使用 postman 我可以生成一个有效的 post。我确实有一种强烈的感觉,它与 Content-type
有关
试试这个:
$http.post($scope.url, JSON.stringify("shouldnotfindanyresultsstring"), { headers: {'Content-Type': 'application/x-www-form-urlencoded'} })
或者这个:
$http.post($scope.url, JSON.stringify({search: "shouldnotfindanyresultsstring"}), { headers: {'Content-Type': 'application/x-www-form-urlencoded'} })
如果您想使用 x-www-form-urlencoded
,您需要将数据实际编码为字符串。 Angular 始终将您的 object 作为 JSON 编码的 object 发布在您的 body 中,即使您指定 header.
在this answer中解释并提供了解决方案
如果您想使用 'application/x-www-form-urlencoded' 然后将您的数据格式化为字符串
data: "search=shouldnotfindanyresultsstring"
如果你想使用 'application/json' 然后使用这个:
var jsonData = { search : "shouldnotfindanyresultsstring" };
$http({
method: 'POST',
url: $scope.url,
contentType: 'application/json',
data: JSON.stringify(jsonData),
}).
success(function (data) {
console.log(data);
}).
error(function (message, status) {
console.log(message);
});
我正在尝试将一些数据与 post 一起发送到特定的 url,后者背后有一个 php 脚本。目前我无法访问 php 脚本。 php 脚本检查字符串是否与数据库中的任何记录匹配,如果匹配则 return 记录。如果没有匹配,脚本将 return 所有记录。
以下代码是我目前所拥有的。如您所见,我有一个字符串 命名:不应该找到任何结果字符串。这实际上应该不会 return 任何结果。但是它 return 是所有记录而不是没有记录。
我尝试了什么:
- 使用
params
代替data
- 使用不同的
Content-types
- 使用 post 方法的简短版本
$http({
url: $scope.url,
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {search: "shouldnotfindanyresultsstring"}
}).then(function (response) {
console.log(response);
}, function (response) { // optional
console.log("Still not working");
});
所以最终我想用搜索字符串在数据库中搜索记录。但是我没有让它工作。
使用 postman 我可以生成一个有效的 post。我确实有一种强烈的感觉,它与 Content-type
试试这个:
$http.post($scope.url, JSON.stringify("shouldnotfindanyresultsstring"), { headers: {'Content-Type': 'application/x-www-form-urlencoded'} })
或者这个:
$http.post($scope.url, JSON.stringify({search: "shouldnotfindanyresultsstring"}), { headers: {'Content-Type': 'application/x-www-form-urlencoded'} })
如果您想使用 x-www-form-urlencoded
,您需要将数据实际编码为字符串。 Angular 始终将您的 object 作为 JSON 编码的 object 发布在您的 body 中,即使您指定 header.
在this answer中解释并提供了解决方案
如果您想使用 'application/x-www-form-urlencoded' 然后将您的数据格式化为字符串
data: "search=shouldnotfindanyresultsstring"
如果你想使用 'application/json' 然后使用这个:
var jsonData = { search : "shouldnotfindanyresultsstring" };
$http({
method: 'POST',
url: $scope.url,
contentType: 'application/json',
data: JSON.stringify(jsonData),
}).
success(function (data) {
console.log(data);
}).
error(function (message, status) {
console.log(message);
});