
[英]How to pass 2 variables from jquery AJAX to ASP.Net WebApi2 method?
[英]Loading tabs with Jquery / Ajax and WebApi Get Method
我正在学习jquery / ajax并进行一些测试。 我试图从asp.net WebApi应用程序提供html,并在用户选择所需的标签时将其加载到JqueryUI标签中。
我无法加载我的HTML内容。
我正在关注此链接上的教程
这是我正在使用的html:
<!DOCTYPE html>
<html>
<head>
<title> Este es el pedazo de titulo</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/ui.tabs.closable.min.js"></script>
<link rel="stylesheet" href="http://jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css">
<link rel="stylesheet" href="http://jqueryui.com/jquery-wp-content/themes/jquery/css/base.css?v=1">
<script>
$(document).ready(function(){
$(function() {
$( "#tabs" ).tabs({
beforeLoad: function( event, ui ) {
ui.jqXHR.error(function() {
ui.panel.html(
"Couldn't load this tab. We'll try to fix this as soon as possible. " +
"If this wouldn't be a demo." );
});
}
});
});
});
</script>
</head>
<body>
<div id="tabs">
<ul>
<li><a href="#tabs-1">Preloaded</a></li>
<li><a href="http://localhost/RestFulApi/api/Tabs?name=carlos">Tab 2</a></li>
</ul>
<div id="tabs-1">
<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
</div>
</div>
</body>
</html>
如您所见,我修改了Tab2
上的href以从WebApi服务加载
在webApi服务内部,我正在使用以下函数,该函数可以毫无问题地返回html:
public class TabsController : ApiController
{
//
// GET: /Tabs/
public HttpResponseMessage Get(string name)
{
string div= "<div style=\"color:white;background-color:red;width:350px;height:300px\"> Hello " +
name+ "<br>This html have being server from webApi!</div>";
return new HttpResponseMessage
{
Content = new StringContent(div, Encoding.UTF8, "text/html")
};
}
}
问题是,每当我单击tab2时,都会收到预定义的错误消息:
Couldn't load this tab We'll try to fix this as soon as possible.
知道为什么我的标签没有被HTML响应填充吗?
编辑:
好的,我在之后添加了console.log(arguments)
ui.jqXHR.error(function() {}
我收到以下错误:
XMLHttpRequest无法加载
http://mydomain/RestFulApi/api/Tabs?name=carlos
。 Access-Control-Allow-Origin不允许使用Origin null。
在对Access-Control-Allow-Origin问题进行了一些研究之后,我通过在IIS服务器上执行应用程序并更改href路径来解决了该问题。
到以下内容:
<li><a href="/RestfulApi/api/Tabs?name=carlos">Tab 2</a></li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.