title: MVC5 Retrieve data with ajax, return json object instead of view
body: <p>I have a simple function that searches for item I want in my database and retrieves it in my controller.</p>
  <pre><code>[HttpPost]
  public ActionResult Index(string searchString)
  {
      var user = from m in db.Users select m;
  
      if (!String.IsNullOrEmpty(searchString))
      {
          user = user.Where(s =&gt; s.UserName.Contains(searchString));
      }
  
      return View(user);
  }
  </code></pre>
  <p>And then in my Javascript I send a value to search:</p>
  <pre><code>$('#test').click(function(e) {
      e.preventDefault();
  
      var user = &quot;John&quot;;
  
      $.ajax({
          url: &quot;@Url.Action(&quot;Index&quot;, &quot;Users&quot;)&quot;,
          data { &quot;searchString&quot;: user },
          type: &quot;post&quot;,
          success: function (saveResult) {
              console.log(saveResult);
          },
          error: function(xhr, ajaxOptions, thrownError) {
              console.log(xhr, ajaxOptions, thrownError);
          }
      })
  })
  </code></pre>
  <p>However of course all this does it return my view inside the console window which is something like:
  <a href="https://i.stack.imgur.com/zejP2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zejP2.png" alt="enter image description here" /></a></p>
  <p>But I would like to return a <code>json</code> object I can use.</p>