Wednesday, 18 January 2012

Asp.net mvc 2 sending Json request to external url and returning value from it.

$(document).ready(function () {
   $("#Submit1").click(function () {         
      var txtvalue= $("#txtvalue").val();
      var txtvalue2= $("#txtvalue2").val();
      var urlStr = "&txtvalue=" + txtvalue+ "txtvalue2=" + txtvalue2;
      $.getJSON("http://localhost:5555/Home/Index?format=json&callback=?" + urlStr, function (data) {
          alert(data);
      });
   });
});

The important point of note here is that you will not be going to get back any value due to the restriction by the browser to stop the xss without specifying callback=? in the url which will result in JsonP call (not a plain JSON call) which is used for calling the external urls.

Method which is going to serve the json request.
public ActionResult Index(FormCollection form)
{
   var data = new { Value ="true"};
   return Json(data,JsonRequestBehavior.AllowGet);
}

Use jquery 1.5 or greater version.

No comments:

Post a Comment