将带有自定义标头的数据发布到Web API的问题 [英] Issue with posting data with custom header to web API

查看:74
本文介绍了将带有自定义标头的数据发布到Web API的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个网络api,其中有一个Post方法。

所以来自另一个网页我是使用jquery ajax将json数据发布到此API,如下所示 -

 $。ajax({
type: POST
url: http:// localhost:4046 / Values / Mymethods
数据:jsondata,
dataType:' json'
})。complete( function (msg){
});



这样工作正常并从API获得结果。



当我在下面添加自定义标题时会出现问题 -

 $。ajax ({
类型: POST
url: http:// localhost:4046 / Values / Mymethods
data:jsondata,
dataType:' json'
header:{' 令牌'' asasaad' }
})。完成( function (msg){
});



现在这会抛出如下所示的跨域错误 -



XMLHttpRequest无法加载http:// localhost:4046 / Values / My方法。请求的资源上不存在Access-Control-Allow-Origin标头。因此不允许来源'http:// localhost:2769'访问。响应的HTTP状态代码为405.



我提供了< add name =Access-Control-Allow-Originvalue =*/> web.config中的标题



我在MVC 4中创建了Web API。



希望有人有一个解决方案。



提前致谢。



我尝试了什么:



尝试提供

< add name =Access-Control-Allow-Originvalue =*/> web.config中的标题

解决方案

.ajax({
type: POST
url: http:// localhost:4046 / Values / Mymethods
数据:jsondata,
dataType:' json'
})。complete( function (msg){
});



这样工作正常并从API获得结果。



当我在下面添加自定义标题时会出现问题 -

 

.ajax({
type: POST
url: http:// localhost:4046 / Values / Mymethods
data:jsondata,
dataType:' json'
header: {' 令牌'' asasaad'}
})。complete( function (msg){
});



现在这会抛出如下所示的跨域错误 -



XMLHttpRequest无法加载http:// localhost: 4046 /价值观/我的方法。请求的资源上不存在Access-Control-Allow-Origin标头。因此不允许来源'http:// localhost:2769'访问。响应的HTTP状态代码为405.



我提供了< add name =Access-Control-Allow-Originvalue =*/> web.config中的标题



我在MVC 4中创建了Web API。



希望有人有一个解决方案。



提前致谢。



我尝试了什么:



尝试提供

< add name =Access-Control-Allow-Originvalue =*/> web.config中的标题


Hi All,

I have a web api which has a Post method in it.
So from another webpage i'm posting json data to this API using jquery ajax as below -

$.ajax({  
    type: "POST",  
    url: "http://localhost:4046/Values/Mymethods",  
    data: jsondata,  
    dataType: 'json'  
}).complete(function (msg) {  
});


This is working fine and getting the result from API.

The issue happens when I add an custom header in to this as below -

$.ajax({  
             type: "POST",  
             url: "http://localhost:4046/Values/Mymethods",  
             data: jsondata,  
             dataType: 'json',  
             header: { 'Token': 'asasaad' }  
         }).complete(function (msg) {  
         });


Now this is throwing cross domain error like below -

XMLHttpRequest cannot load http://localhost:4046/Values/My methods. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:2769' is therefore not allowed access. The response had HTTP status code 405.

I have provided <add name="Access-Control-Allow-Origin" value="*" /> header in web.config

I have created Web API in MVC 4.

Hope someone has got an solution for this.

Thanks in advance.

What I have tried:

Tried providing

<add name="Access-Control-Allow-Origin" value="*" /> header in web.config

解决方案

.ajax({ type: "POST", url: "http://localhost:4046/Values/Mymethods", data: jsondata, dataType: 'json' }).complete(function (msg) { });


This is working fine and getting the result from API.

The issue happens when I add an custom header in to this as below -


.ajax({ type: "POST", url: "http://localhost:4046/Values/Mymethods", data: jsondata, dataType: 'json', header: { 'Token': 'asasaad' } }).complete(function (msg) { });


Now this is throwing cross domain error like below -

XMLHttpRequest cannot load http://localhost:4046/Values/My methods. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:2769' is therefore not allowed access. The response had HTTP status code 405.

I have provided <add name="Access-Control-Allow-Origin" value="*" /> header in web.config

I have created Web API in MVC 4.

Hope someone has got an solution for this.

Thanks in advance.

What I have tried:

Tried providing

<add name="Access-Control-Allow-Origin" value="*" /> header in web.config


这篇关于将带有自定义标头的数据发布到Web API的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆