Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
2
Reply
Web API Using JQuery AJAX In ASP.NET MVC 5 getting error
Rupesh Kahane
Nov 30 2017 6:46 AM
237
Web API Using JQuery AJAX In ASP.NET MVC 5 getting error "
Invalid or unexpected token
"
I have created a Web Api project with MVC. Which contains HomeController having Action method index
public
ActionResult Index()
{
return
View();
}
For above action method I would like to show employee details from ajax call in javascript alert message as
@{
Layout
=
null
;
}
<
script
src
=
"~/Script/jquery-1.10.2.min.js"
>
script
>
>
<
html
>
<
head
>
<
meta
name
=
"viewport"
content
=
"width=device-width"
/>
<
title
>
Index
title
>
head
>
<
body
>
<
div
>
<
h2
>
You Are On View
h2
>
div
>
body
>
html
>
<
script
>
$(document).ready(function () {
$.ajax({
type: 'GET',
url: "http://localhost:802/api/Employee/GetEmployee?
Id
=
101
",
contentType: "application/json;
charset
=
utf
-8",
dataType: "jsonp",
success: function (data) {
alert(data);
},
failure: function (data) {
alert("Failure: "+ data);
},
error: function (data) {
alert("Error: "+ data);
}
});
Also I have created one api controller as follows, which will return employee details
public
class
EmployeeController : ApiController
{
public
Employee GetEmployee(
int
Id)
{
Employee objEmp =
new
Employee();
objEmp = ;
// get data from service
return
objEmp;
}
}
I tried below solution
http://www.c-sharpcorner.com/article/call-web-api-using-jquery-ajax-in-asp-net-core/
I am getting error like "
Uncaught SyntaxError: Invalid or unexpected token
" in console.
If I click on the error in console then I am getting my result as
{ "EmpId": "101","Name": "Rupesh" }
Reply
Upload Source Code
Select only zip and rar file.
Post
Reset
Answers (
2
)
Display currency symbol in c# in pdf
pass radio button value to controller without submit button