1

I have the following WebMethod :-

        [WebMethod(EnableSession = true)]
    public static void OpenReport(string reportName)
    {
        Report report = reportsBll.GetReports().FirstOrDefault(x => reportQuestion != null && x.Id == reportQuestion.ReportId);

        if (report != null) reportUrl = report.Url;
    }

Now I wish to pass the report.Url to this Jquery Method :-

    $('.ReportButton').click(function () {
    var args = { reportName : '' };

    $.ajax({
        type: "POST",
        url: "ReportsByQuestionsDetails.aspx/OpenReport",
        data: JSON.stringify(args),
        contentType: "application/json;charset=utf-8;",
        success: function (data) {
            alert('success');
            document.location.href = reportUrl;
        },
        error: function () {
        }
    });
});

How can I pass the reportUrl from the WebMethod to the Jquery?

Thanks for your help and time

6
  • 3
    Are you aware of what the void keyword in your c# method means? Commented Aug 8, 2013 at 14:29
  • Yes I have to change it to return the URL, however I have no idea on how to pass it to Jquery Commented Aug 8, 2013 at 14:31
  • Try checking the value of data in your jQuery success function after changing your webmethod. Commented Aug 8, 2013 at 14:32
  • I tried to check the value of data, its object Object Commented Aug 8, 2013 at 14:36
  • Do console.log(data), not an alert. Commented Aug 8, 2013 at 14:39

2 Answers 2

5

You need to return the string in your C# method:

[WebMethod(EnableSession = true)]
public static string OpenReport(string reportName)
{
    string reportUrl = string.Empty;
    Report report = reportsBll.GetReports().FirstOrDefault(x => reportQuestion != null && x.Id == reportQuestion.ReportId);

    if (report != null) reportUrl = report.Url;

    return reportUrl;
}

Then in your ajax return Url you can do (you might want a fallback for when the report is null however):

success: function (data) {
    location.href = data;
},
Sign up to request clarification or add additional context in comments.

3 Comments

@Johann If you debug the WebMethod, does the breakpoint hit? Is the report definitely not null?
yes i am getting a url, just debugged. Redirecting to localhost/myapp/[object Object]
@Johann is there anything in data.d in your success method? Try alerting it. If so, use that.
1

You need to change your page method to actually return something, you currently have it returning nothing via the void return type, change it to this:

[WebMethod(EnableSession = true)]
public static string OpenReport(string reportName)
{
    Report report = reportsBll.GetReports().FirstOrDefault(x => reportQuestion != null && x.Id == reportQuestion.ReportId);

    if (report != null)
    { 
        return report.Url;
    }

    return String.Empty;
}

UPDATE: Microsoft added a parent container for JSON responses in ASP.NET AJAX 3.5 in an effort to combat potential cross-site scripting (XSS) attacks; therefore you success callback needs to be this:

success: function (data) {
    alert('success');
    document.location.href = data.d;
}

There are ways to mitigate this as described in Never worry about ASP.NET AJAX’s .d again

13 Comments

You still return nothing if report is null.
@Johann - your success callback needs to change its logic to use document.location.href = data; instead of document.location.href = reportUrl;. data is what you returned from the page method, in this case a string.
Karl yes at the moment its document.location.href = data; Still not working though
Then your reportsBll.GetReports() is returning invalid data for the URL, did you debug it to see what the value of the URL is?
I am definelty getting the Url, I just debugged. Its redirecting to this page localhost/myapp/[object Object]
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.