views:

14

answers:

1

I can not send data to MVC controller using YAHOO connect library.

Parameters query and filter are NULL. Where is the problem?

   // --- JavaScript --- //
    var callbacks = {
        // Successful XHR response handler 
        success: function (o) {
            var messages = [];
            // Use the JSON Utility to parse the data returned from the server 
            try {
                messages = YAHOO.lang.JSON.parse(o.responseText);
            }
            catch (x) {
                alert("JSON Parse failed!");
                return;
            }
            handleSearchResult(messages, query, filter);
        },
        argument: { query: "flowers", filter: "home" } 
    };

    // Make the call to the server for JSON data 
    YAHOO.util.Connect.asyncRequest("GET", "Search/GetTopics", callbacks);

    // --- C# --- //
    //Controller
    [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult GetTopics(string query, string filter)
    {
       // query and filter are NULL <- problem here // 
       // ...do my stuff... //
       return Json(Search(query, filter), JsonRequestBehavior.AllowGet);
    }

Thank you! :)

A: 

You have to possibilities to send parameters:

  1. Use GET verb: In this case you need to pass the parameters in the querystring:

    YAHOO.util.Connect.asyncRequest('GET', 
        'Search/GetTopics?query=foo&filter=bar', callbacks);
    
  2. Use POST verb: In this case you could use the postData parameter

    YAHOO.util.Connect.asyncRequest('POST', 'Search/GetTopics', 
        callbacks, 'query=foo&filter=bar');
    

In the first case it is actually recommended to use Url helpers to generate the address to make sure values are properly url encoded:

var url = '<%= Url.Action("GetTopics", "Search", new { query = "foo", filter = "bar" }) %>';
YAHOO.util.Connect.asyncRequest('GET', url, callbacks);

The same is true for the second case. Make sure to properly encode values.

Darin Dimitrov