1

I'm fairly new to asp.net MVC but am baffled as to why my request isn't working.

I'm trying to send an ajax request with jquery as per:

jQuery(function ($) {
    var total = 0,
        share = $('div.share'),
        googlePlusUrl = "https://plusone.google.com/_/+1/fastbutton?url=http://bookboon.com" + $(location).attr('pathname');

    setTimeout(function() {
        $.ajax({
            type: 'GET',
            data: "smelly",
            traditional: true,
            url: share.data('proxy'),
            success: function(junk) {
                //var $junk = junk.match(regex);
                console.log(junk);
            },
            error: function (xhr, errorText) {
                console.log('Error ' + xhr.responseType);
            },
        });
    }, 4000);

And set a line in my RouteConfig as:

routes.MapRoute(null, "services/{site}/proxy", new { controller = "Recommendations", action = "Proxy" });

The markup has a data-attribute value as:

<div class="share" data-proxy="@Url.Action("Proxy", "Recommendations")">

And my Proxy action method starts with:

public ActionResult Proxy(string junk)

The problem is that the junk parameter is always null. I can see in the debug output that the route seems to correctly redirect to this method when the page loads (as per jQuery's document ready function), but I cannot seem to send any data.

I tried sending simple data ("smelly") but I don't receive that neither.

Any suggestions appreciated!

1 Answer 1

2

The model binder will be looking for a parameter in the request called junk, however you're sending only a plain string. Try this:

$.ajax({
    type: 'GET',
    data: { junk: "smelly" }, // <- note the object here
    traditional: true,
    url: share.data('proxy'),
    success: function(junk) {
        //var $junk = junk.match(regex);
        console.log(junk);
    },
    error: function (xhr, errorText) {
        console.log('Error ' + xhr.responseType);
    },
});
Sign up to request clarification or add additional context in comments.

1 Comment

That alacrity in response and you bothered to correct the spacing.. have all of my babies good sir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.