I have a ASP.NET MVC3 application that gets a List of Coordinates from a WCF service. This service returns a List with a few properties (Xcoor and Ycoor).

   public List<Location> GetCoordinate()
        {
            sensor = new Location();

            List<Location> coordinateList = sensor.GetSensorInformation();

            return coordinateList;

        }

In my web Page, I have a JavaScript function that takes 2 parameters and displays coordinates on the page (on a map)

function GetLocation(y, x){
//my code here
}

I want to get the list of coordiates from my controller and pass them in to that JS function for processing.

What is the best way to go about this?

I tries using the following but I dont know how to parse the results

 $.ajax({
            url: '@Url.Action("GetCoordinate")',
            type: 'GET',
             dataType: 'xml',
            success: function(result) {

            },
            error: function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);}
        });

Any ideas?

share|improve this question
1  
If GetCoordinatesArray returns a List<T> how exactly will jQuery's $.ajax invoke that as a URL? – Only Bolivian Here May 3 '12 at 17:26
1  
Use JSON....... – Travis J May 3 '12 at 17:26
    
Sergio, my typo. I updated the post. Thanks. Assume that i gets to sccess and function(result). @Travis, how do I use JSON for this? – Mage May 3 '12 at 17:30
up vote 6 down vote accepted

Change your controller to the following:

public ActionResult GetCoordinate()
{
    sensor = new Location();

    List<Location> coordinateList = sensor.GetSensorInformation();

    return Json(coordinateList, JsonRequestBehavior.AllowGet);

 }

Now your controller action is returning JSON which jQuery can process.

Change your jQuery method to do this:

$.ajax({
    url: '@Url.Action("GetCoordinate")',
    type: 'GET',
    dataType: 'json',
    success: function(data) {
        // process the data coming back
        $.each(data, function(index, item) {
            alert(item);
        });
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

Now your AJAX method knows it is dealing with JSON data. You can use the jQuery each method to do something with the data (I am just doing an alert in this case).

share|improve this answer
    
"item" returns object Object. How to get my the actual data from the list? – Mage May 3 '12 at 17:45
    
Ok I see the data...Thanks. Ill plug that guy into my JS function – Mage May 3 '12 at 17:51
    
Worked liked a charm! Thanks for your help!!!! :) – Mage May 3 '12 at 18:05
    
@Mage You're welcome! – Dismissile May 3 '12 at 19:10

well there a bunch of things here.

Define a function on your controller define a method

public JsonResult GetCoodinates() { }

consume your WCF server in the method and convert the results to a JsonResult.

In your javascript

$.ajax({ url: '/controller/GetCoordinates', type: GET, dataType: 'json', success: function(data) { $.each(data, function (key, value) { //do whatever call you are doing in here to place the coordinates on the map } });

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.