how to use Ajax with ASP.NET MVC4 Enterprise application - asp.net

i am going to develop a new application and i have decided to use asp.net mvc4 framework.
my project is very large and i would to make a professional work,now my problem is how to use ajax for such an application.
when i tried to think to use j query ajax i found that i'll make ajax request for each entry form and it would take more time.
<script type="text/javascript">
$(document).ready(function () {
var sURL = '/AjaxTest/FirstAjax';
$.ajax({
type: "POST",
url: sURL ,
data: {
//Form data to submit to controller .....},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
alert(data);
}
function errorFunc() {
alert('error');
}
});
</script>

You can make some service function that does ajax requests and receives url and data to send.
dataService=function(url,data)
{
return $.ajax({
type: "POST",
url: url ,
data: data
contentType: "application/json; charset=utf-8",
dataType: "json",
});
};
Your dataservice method (ajax requests) returns promise object which means you can do something like:
dataService("firstUrl","firstAjaxCallData")
.then(function(){ console.log("this happens after sucessfull ajax call")});

Related

How to Get the "Request PayLoad" data using vb.net

I am using vb.net 2.0 and asp.net in my project.
I am sending ajax request from a page to get response.
$.ajax({
type: "POST",
url: "GetMyData.aspx?i=2",
data: '{ugData: \"10\"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
return false;
},
failure: function (response) {
alert("Failed");
}
});
In this code, after ajax request I can see that the variable "ugData" is in the Request Payload in Developers tool.
Can any one help me out How can i able to read this "ugData" variable value in my Page "GetMyData.aspx".
Thank You..

Send AJAX request to .aspx page and return JSON

I know that it is possible to send an AJAX request to an .asmx page. And I also know that an .asmx page handles an AJAX request via a web method.
Is it also possible to send an AJAX request to an .aspx page? If so, does an .aspx page also handle an AJAX request via a web method? Note that I would like to return a JSON response from the .aspx page. Is this possible?
You can define web methods in the code-behind of your .aspx page and then call them:
[WebMethod]
public static string doSomething(int id)
{
...
return "hello";
}
And then, to call a web method in your jQuery code:
$.ajax({
type: "POST",
url: "YourPage.aspx/doSomething",
data: "{'id':'1'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
var returnedstring = data.d;
var jsondata = $.parseJSON(data.d);//if you want your data in json
}
});
Here is a good link to get started.
if i understood question correctly, Aspx is same as HTML. It will be rendered as HTML. but only difference is Server Side and Controls retaining the states with state mechanism.
so you can do jquery $.ajax() function.
$.ajax({
url: UrlToGetData,
dataType:'json',
success:function(data){
//do some thing with data.
}
});
or if you want to write out json value to the response, then use Response.ContentType
first use any Javascript serializer(JSON.NET) , then set the contentType like this.
Response.ContentType="application/json";
$.ajax({
url: "(aspx page name/method to be called from the aspx.cs page)",
type: "POST",
dataType: "json",
data: $.toJSON(jsonData),
contentType: "application/json; charset=utf-8",
success: function (data, textStatus, jqXHR) {
//TO DO after success
}
});
Try the above code

webservice not returning values to on jquery ajax request

I would like consume cross-domain web-service from client with jquery.here is my code
function getId() {
var testid = ($('#<%=PreviousTest.ClientID %> OPTION:selected').val());
jQuery.support.cors = true;
jQuery.ajax({
type: "POST",
url: "../FalconWebService.asmx/minlatency",
data: "{'testId':" + testid + "}",
contentType: "application/json; charset=utf-8",
dataType:"json",
success: function (data) {
alert("catch");
var msg = jQuery.parseJSON(data.Table);
return msg;
},
Error: function () {
alert("error");
}
my webservice returns values in this format
{"Table":[{"minlatency":16.0,"Time":"/Date(1328248782660+0530)/"},{"minlatency":7.0,"Time":"/Date(1328248784677+0530)/"},{"minlatency":13.0,"Time":"/Date(1328248786690+0530)/"},{"minlatency":6.0,"Time":"/Date(1328248788690+0530)/"},{"minlatency":20.0,"Time":"/Date(1328248790707+0530)/"},{"minlatency":12.0,"Time":"/Date(1328248792723+0530)/"},{"minlatency":26.0,"Time":"/Date(1328248794723+0530)/"},{"minlatency":18.0,"Time":"/Date(1328248796723+0530)/"}]}
Calls in cross-domain work in a different way. They create dynamic javascripts that are inserted to the page using a callback function. This function is used to handle the response from the service. The Jquery calls add a "?callback=?" to the URL of the service, where "callback" is the name of the function that will be inserted.
To call cross-domain services with JQuery you have to do the following:
jQuery.ajax({
type: "POST",
url: "../FalconWebService.asmx/minlatency",
data: "{'testId':" + testid + "}",
contentType: "application/json; charset=utf-8",
dataType: "jsonp", //The data type that you must use is JSONP. Basically tells JQuery that the request is cross-domain
success: function (data) {
alert("catch");
var msg = jQuery.parseJSON(data.Table);
return msg;
},
Error: function () {
alert("error");
},
jsonpCallback: 'callback' //Dude to the fact that the JS is being generated dynamicaly, this tells JQuery to use the name "callback" for the function that will handle the result, as it adds "?callback=?" to the URL
});
You could also use "jsonp" instead of "jsonpCallback" if you want to "override the callback function name in a jsonp request" (from JQuery).
It worked for me (in WCF REST services which communicate with JSON messages, but it should work the same way in your case). This is all explained in the JQuery Ajax documentation.
Hope this helps.

Novice with jQuery AJAX

I am simply learing Ajax with jQuery and have a simple page method that takes in a parameter and returns a string. For some reason, I am not getting the return string but I am getting to my 'success' message:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "testFormMatt.aspx/sayHello",
contentType: "application/json; charset=utf-8",
data: '{"name": "matt"}',
dataType: "json",
success: function(msg) {
$.jGrowl('The web service has been successfully called');
$('#result').append(msg);
}
});
});
When you call append, you need to specify the property of the JSON object that you want to append.
So if your page is returning:
{ message: "Hello, Matt" }
Then you'd need to call append like this:
$("#result").append(msg.message);
If your page is not returning JSON, then you need to take the dataType: "json" out of the $.ajax call. The dataType parameter is for specifying the expected data type of the response, not the data type of the request.

calling a webservice with jquery that accepts five STRING params?

is it possible to call a webservice that accepts 5 string parameters without sending via json? (is this recommended) I have created a webservice with a method that accepts 5 string params.. and i have my jquery
$.ajax({
type: "POST",
url: "Service.aspx/CreateClient",
data: "{}",
contentType: "application/json; charset=utf-8", //// ERMMM ???
dataType: "json", // ERMMM?
success: function(msg) {
alert(msg.d);
},
error: function() {
alert('error');
}
});
The old way without using jquery was to do this
this.para.add("Name", name);
this.para.add("ClientNum", clnum);
this.para.add("Email", email);
this.para.add("Register", register);
this.para.add("Message", message);
SOAPClient.invoke(this.url, "MyService.aspx/CreateClient", this.para, true, this.completeDone, this);
At the other end it lands and fills in all parameters....
What is the recommended way?
why not use querystring variables and avoid the overhead of the SOAP protocol?

Resources