I have written simple webapi method. I am trying to access that method in mvc using ajax call. I am getting output in browser and fiddler . But Its not working in cshtml page.Please let me know , If I have to add something. Please look into this..
function validate() {
alert("came");
$.ajax({
url: 'http://localhost:54544/api/home/getname/',
method: 'GET',
contentType: 'application/form-url-encoded',
success: function ()
{
alert("success");
},
error: function (ex) {
alert(ex.responseText);
}
});
}
check the html code here
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>View</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
// beforeSend: function(xhr, settings) { xhr.setRequestBody('Authorization','Bearer ' + token); } ,
$(document).ready(function () {
alert("came");
$.ajax({
url: "http://localhost:54544/api/home/getname/",
type: "Get",
success: function (data) {
alert("test came");
},
error: function (msg) { alert("error"); alert(msg); }
});
});
</script>
</head>
<body>
<div>
<table>
<tbody>
<tr>
<td><input type="button" value="Submit" onclick="validate()" name="Submit" /> </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

cshtmlandLayoutpage, did you wrap it in section? Because by the default template, if you call<script></script>immediately in yourcshtmlwithout wrapping it in section, it will be called before thejquery.jsand will cause error because thejquery.jsis being called afterRenderBody()in theLayoutpage