Tân
Tân

Reputation: 1

How to make a POST request via Redirect in mvc?

From Controller, is there a way to send a POST request to specific domain with parameter?

What I wanna archive looks like this:

public ActionResult Index()
{
   Redirect("https://www.anothersite.com", new { s = "abc" });
}

I wanna do this in server side instead of using ajax in client side:

$.ajax({
   url: 'https://www.anothersite.com',
   type: 'POST',
   data: { s: 'abc' }
}).done(function (data) {
   // logic...
})

Is it possible?

Upvotes: 3

Views: 3681

Answers (1)

Shyju
Shyju

Reputation: 218942

When you return a RedirectResult using Redirect method, It will issue a new GET request to the url.

Also, The syntax you had is wrong. It should bereturn Redirect("https://www.anothersite.com");

If you still want to do a POST request to another domain, what you can do is, set the form action method to the other domains url, set the method as "POST" and submit the form and that will do a form submit to the new page.

<form action="http://www.stackoverflow.com" method="POST">
    <input type="submit" />
</form>

If you want a POST request from your server side code, you should consider using HttpClient class which can make a Post/Get request with some data to another domain /web service. Since this happens in the code, your browser will still have your previous page from the code is invoked. Typically this solution is used to talk to web services/rest api's to get/post data.

Upvotes: 4

Related Questions