【发布时间】:2017-11-08 08:22:01
【问题描述】:
我已经使用跨域将一个域调用到另一个域,如下所示,
我的来自 mvc 项目的 ajax 调用如下所示,
var tableparameter = '{TableName: "' + $("#tableOneTableName").val() + '" }';
$.ajax({
type: "POST",
url: "http://localhost:55016/api/ajaxapi/onchangetableone",
data: tableparameter,
success: function (response) {
alert("hi");
}
});
然后我在 web api 项目的 web.config 文件中添加了以下代码,如下所示,
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept,Authorization" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
然后我写了如下的动作,
[HttpPost]
[Route("api/ajaxapi/onchangetableone")]
public List<DropDownListValuesForQuery> OnChangeTableOne(TableNameFormCustomFilters TableParameter)
{
CaseModel query = new CaseModel();
List<DropDownListValuesForQuery> listValues = new List<DropDownListValuesForQuery>();
listValues = query.GetColumnNames(TableParameter.TableName);
return listValues;
}
并列出如下值,
public class TableNameFormCustomFilters
{
public int TableName { get; set; }
}
这里我可以使用跨域访问 webapi 源,但参数值不是来自脚本。请提出您的建议。
提前谢谢.....
【问题讨论】:
-
更改你的 Js 代码 var tableparameter = {"TableName": $("#tableOneTableName").val() };
标签: c# jquery ajax asp.net-mvc cross-domain