我是angular.js的新手,我正在尝试为请求添加一些标头:
var config = {headers: {
'Authorization': 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==',
'Accept': 'application/json;odata=verbose'
}
};
$http.get('https://www.example.com/ApplicationData.svc/Malls(1)/Retailers', config).success(successCallback).error(errorCallback);
Run Code Online (Sandbox Code Playgroud)
我查看了所有文档,在我看来这应该是正确的.
当我在网址中使用本地文件时$http.get,我会在Chrome的网络标签上看到以下HTTP请求:
GET /app/data/offers.json HTTP/1.1
Host: www.example.com
Connection: keep-alive
Cache-Control: max-age=0
If-None-Match: "0f0abc9026855b5938797878a03e6889"
Authorization: Basic Y2hhZHN0b25lbWFuOkNoYW5nZV9tZQ==
Accept: application/json;odata=verbose
X-Requested-With: XMLHttpRequest
If-Modified-Since: Sun, 24 Mar 2013 15:58:55 GMT
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
X-Testing: Testing
Referer: http://www.example.com/app/index.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Run Code Online (Sandbox Code Playgroud)
如您所见,两个标头都已正确添加.但是当我将URL更改为$http.get上面显示的URL时(除了使用真实地址,而不是example.com),我得到:
OPTIONS /ApplicationData.svc/Malls(1) HTTP/1.1
Host: www.datahost.net
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://mpon.site44.com
User-Agent: …Run Code Online (Sandbox Code Playgroud) 我从angular.js客户端向asp.net web api PUT方法发出以下请求:
var org = {
OrgId: 111,
name: 'testing testing'
};
$http.put("http://localhost:54822/api/data/putorganisation/1/", org).then(function(status) {
console.log("success PUT");
return status.data;
});
Run Code Online (Sandbox Code Playgroud)
但是获得以下errormsg(在fiddler中):
{"message":"The requested resource does not support http method 'OPTIONS'."}
Run Code Online (Sandbox Code Playgroud)
这是我的asp.net web api web.config文件的一部分:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type,x-xsrf-token,X-Requested-With" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<remove name="WebDAV" /> …Run Code Online (Sandbox Code Playgroud) 我希望每个人都做得很好.我最近开始使用angular 4.4,我一直试图将数据发布到我的api服务器,但不幸的是它没有用.我花了两天时间,但仍然没有成功.并且已经尝试过6-7篇甚至来自angular.io的文章.我已经尝试了Http和Httpclient模块,但似乎没有任何工作.
问题是,每当我尝试将数据发布到我的服务器时,Angular都会生成http OPTIONS类型请求而不是 POST.
this.http.post('http://myapiserver.com', {email: 'adam@example.com'}).subscribe(
res => {
const response = res.text();
}
);
Run Code Online (Sandbox Code Playgroud)
我也试图发送请求的自定义选项,但仍然没有成功.
const headers = new Headers({ 'Content-Type': 'x-www-form-urlencoded' });
const options = new RequestOptions({ headers: headers });
options.method = RequestMethod.Post;
options.body = {name: 'Adam Smith'};
//options.body = JSON.stringify({name: 'Adam Smith'}); // i also tried this
//options.body = 'param1=something¶m2=somethingelse'; // i also tried this
Run Code Online (Sandbox Code Playgroud)
我使用的是ASP.NET核心2.0,但由于它不起作用我也试过简单的PHP代码,这里是php的服务器端代码.它也没有用.
<?php
print_r($_POST);
?>
Run Code Online (Sandbox Code Playgroud)
注意:Cors也在服务器上启用.另外,我也试过简单的get请求,它的工作非常好.
我真的很感激一些帮助.
提前致谢
javascript http-post angular-http angular angular-httpclient
我有一个 ASP.NET WebAPI 客户端,它具有以下配置,允许任何跨域POST和GET调用:
public static void Register(HttpConfiguration config)
{
config.EnableCors(new EnableCorsAttribute("*", "*", "GET,POST"));
// other unrelated configurations
}
Run Code Online (Sandbox Code Playgroud)
我对我的 WebAPI 应用程序进行了 AJAX 调用,所有这些调用都失败了,并在控制台中显示了 CORS 预防消息。
以下 AJAX 请求失败:
$.ajax({
url: "myserver:8081/User/Get",
data: {
"Id": 12
},
type: "POST",
dataType: "json",
contentType: "application/json; encoding=utf-8"
});
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况以及如何解决这个问题?
我正在使用express-jwt来构建一个宁静的 api。现在客户端正在进行重复的ajax调用,第一个发起者是angularjs,第二个发起者是other。第一个得到 204 作为响应代码,第二个得到 200 作为响应代码。我试图调试以找到这个重复请求的来源,但我不能。
以下是带有 204 状态代码的标题的详细信息
以下是带有 204 状态代码的标题的详细信息
任何人都可以建议可能是什么问题吗?
ajax ×3
angularjs ×3
javascript ×3
cors ×2
angular ×1
angular-http ×1
cross-domain ×1
express-jwt ×1
http-headers ×1
http-post ×1
jquery ×1
node.js ×1
rest ×1
sitecore ×1