Rupesh Kahane

Rupesh Kahane

  • 102
  • 19.1k
  • 4.2m

Web API Using JQuery AJAX In ASP.NET MVC 5 getting error

Nov 30 2017 6:46 AM
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
  1. public ActionResult Index()  
  2.         {  
  3.             return View();  
  4.         }  
  For above action method I would like to show employee details from ajax call in javascript alert message as
 
  1. @{  
  2.     Layout = null;  
  3. }  
  4. <script src="~/Script/jquery-1.10.2.min.js">script>  
  5. >  
  6. <html>  
  7. <head>  
  8.     <meta name="viewport" content="width=device-width" />  
  9.     <title>Indextitle>  
  10. head>  
  11. <body>  
  12.     <div><h2>You Are On Viewh2>div>  
  13. body>  
  14. html>  
  15. <script>  
  16.   
  17. $(document).ready(function () {  
  18.             $.ajax({  
  19.             type: 'GET',    
  20.             url: "http://localhost:802/api/Employee/GetEmployee?Id=101",  
  21.             contentType: "application/json; charset=utf-8",    
  22.             dataType: "jsonp",    
  23.             success: function (data) {            
  24.             alert(data);      
  25.               
  26.             },  
  27.              failure: function (data) {    
  28.                 alert("Failure: "+ data);    
  29.             },   
  30.             error: function (data) {  
  31.                 alert("Error: "+ data);    
  32.             }   
  33.  });  
 Also I have created one api controller as follows, which will return employee details
  1. public class EmployeeController : ApiController  
  2.     {  
  3. public Employee GetEmployee(int Id)  
  4. {  
  5.    Employee objEmp = new Employee();  
  6.    objEmp = ; // get data from service   
  7.    return objEmp;  
  8. }  
  9. }  
 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" }
 
 

Answers (2)