Cross domain ajax request to a json file using JSONP

Jak
Jak
Member
858 Points
132 Posts

Hi,

I am trying to get data from different domain using jsonp of jquery.I am using asp.net MVC on server side.

Ajax call as:

function JoinUser(roomid) {
$.ajax({
type:"post",
url: $.getUrl("controllername/JoinUser"),
data: {'roomid': roomid},
contentType: "application/json;charset=utf-8",
dataType:"jsonp",
success:function (data) {  },
error:function (data, status) {  }
});
}

 

Controller Code:

[AcceptVerbs(HttpVerbs.Get)]
 
public JsonResult JoinUser(string roomid)
return Json("", JsonRequestBehavior.AllowGet);
}

 

But am getting error. Am I missing anything.

 

Views: 9932
Total Answered: 3
Total Marked As Answer: 1
Posted On: 27-Jun-2015 20:50

Share:   fb twitter linkedin
Answers
Smith
Smith
None
2568 Points
74 Posts
         

Hi Jak,

Post the error code.

Posted On: 27-Jun-2015 20:59
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

Hi Jak,

Check the following steps:

I.    In web.config file

Add following section

In <System.web>:

<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>

In <Sytem.WebServer>:

<system.webServer>
<httpProtocol>
<customHeaders>
<!--Enable Cross Domain AJAX calls -->
<remove name="Access-Control-Allow-Origin" />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>

 

II.   In Action method

You need to modify action as:

[AcceptVerbs(HttpVerbs.Get)] 
public string JoinUser(string callback, string roomid)
var JSONString = new JavaScriptSerializer().Serialize("");
 
return callback+"( "+JSONString + " )";
}

 

 

Posted On: 27-Jun-2015 21:22
Jak
Jak
Member
858 Points
132 Posts
         

Hi Rahul,

It's working.

thanks.

Posted On: 29-Jun-2015 23:58
 Log In to Chat