Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I created one Wep API CRUD operation using MVC.
I followed the following link to create that Web API.
Kindly go through this link for clear.
MVC 4 WEB API .NET 4.5

I got the GET response also.
But I am getting XML response.

Instead, I needed json response. So, in that article, they told to put following javaScript to get JSON response, but they did not mention where to put the following javaScript.

So, blindly I put the javaScript in index.cshtml<--views<---HOME(which is in solution explorer in project).

Because this folder only has design form. So, still I am getting XML response.
If I remove the javaScript also, it response XML format.

So, I think the javaScript is not invoked or I do not know where to put the javaScript correctly.

So, please help from this problem.

Thanks a lot.

javaScript file:
C#
<script type="text/javascript">
$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "api/customer/1",
        dataType: "json",
        success: function (data) {
            alert(data);
        }
    });
});
</script>

output file:
XML
<CustomerModel>
    <Id>1</Id>
    <Salary>20</Salary>
    <cusName>rajesh</cusName>
</CustomerModel>
<CustomerModel>
    <Id>2</Id>
    <Salary>50</Salary>
    <cusName>magesh</cusName>
</CustomerModel>
</ArrayOfCustomerModel>




[Edit member="Tadit"]
Link text added to reflect the article title.
Corrected title of question, formatting and grammatical issues.
[/Edit]
Posted
v5

Looks like only xml is supported. Json simply isn't.

Good luck!
 
Share this answer
 
If you are unable to get JSON response, then in that case you can convert your XML response to JSON response in javaScript by using some jQuery plugin.

Look at the link for XML to JSON conversion..
jQuery XML to JSON Plugin[^]

Hope this helps.


[Edit member="Tadit"]
Link text added to reflect the article title.
Corrected formatting and grammatical issues.
[/Edit]
 
Share this answer
 
v2
Use the below code

CONTROLLER


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class App1Controller : Controller
    {
        //
        // GET: /App1/

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetCustomerModelList()
        {
            CustomerModelList oBOCustomerModelList = new CustomerModelList();

            for (int i = 2; i < 10; i++)
                oBOCustomerModelList.Add(new CustomerModel { Id = i, Salary = i * 2 + 300, cusName = "CUST " + i.ToString() });

            return Json(oBOCustomerModelList, JsonRequestBehavior.AllowGet);
        }

    }
}


VIEW

XML
@{
    ViewBag.Title = "Index";
    Layout=null;
}

<script type="text/javascript" src="~/Scripts/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>

    $(function () {
        $('#btnShowAll').click(function () {
            $.ajax({
                type: "GET",
                url: "./App1/GetCustomerModelList",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var content = '<table style="border-spacing:0;" border="1"><trstyle="font-weight:200;><td>ID</td><td>Salary</td><td>Name</td></tr>';
                    $(response).each(function () {
                        content = content + '<tr><td>' + this.Id + '</td><td>' + this.Salary + '</td><td>' + this.cusName + '</td></tr>';
                    });
                    content = content + '</table>';
                    $('#dvData').html(content);
                }
            });
        });
    });

</script>


<input type="button" id="btnShowAll" value="Show All"/>

<div id="dvData"></div>


MODEL


C#
public class CustomerModel
{
    public int Id { get; set; }
    public int Salary { get; set; }
    public string cusName { get; set; }
}

public class CustomerModelList : List<CustomerModel>
{
}
 
Share this answer
 
Make Sure that you have call from controller as per below code

public JsonResult Index(UserViewModel _UserViewModel)
{
return Json(data, JsonRequestBehavior.AllowGet);
}
after that u can check your data it will converted into the json.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900