I am having the below code in my project
$.ajax({
type: "POST",
url: "Alerts.aspx/TestMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnError
});
now I want to pass the "this" or me(vb.net) as parameter in data object how to pass the current class object.
can any one help me
I tried data: "{obj:"<%=Me%>"}",
but it is not working can anyone help ...
Related
I have an Ajax call that works fine, but I am now trying to polish the code a bit
$.ajax({
url: wbURL,
dataType: "xml",
contentType: "text/xml; charset=\"utf-8\"",
type: "POST",
headers: '#"HEADER DATA',
data: dataPacket,
success: dealWithResonse,
error: dealWithError
});
what I am now trying to do is replace the success and error function names with variables so I can use it for other jobs.
var SuccessFunctionCall = dealWithResonse;
var ErrorFunctionCall = dealWithError;
$.ajax({
url: wbURL,
dataType: "xml",
contentType: "text/xml; charset=\"utf-8\"",
type: "POST",
headers: '#"HEADER DATA',
data: dataPacket,
success: SuccessFunctionCall,
error: ErrorFunctionCall
});
But my program stops at this point, assumingly as it can't find a function called
ErrorFunctionCall.
can I use a variables vaule instead of an actual function name?
Thank you in advance
You may define those functions as global and assign them to what you want:
var SuccessFunctionCall = function(){dealWithResonse()};
var ErrorFunctionCall = function(){dealWithError()};
and call them as members of window:
success: window[SuccessFunctionCall](),
error: window[ErrorFunctionCall]()
Finally I have to say that I am not sure why you want to assign functions to variable names. You may define those functions directly and call them as you need.
You should define those functions in your variables as shown below:
$(document).ready(function(){
var SuccessFunctionCall = function dealWithResonse(){
console.log("success");
};
var ErrorFunctionCall = function dealWithError(){
console.log("error");
};
$.ajax({
url: 'http:test',
dataType: "xml",
contentType: "text/xml; charset=\"utf-8\"",
type: "POST",
headers: '#"HEADER DATA',
data: '',
success: SuccessFunctionCall,
error: ErrorFunctionCall
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
Try this
im new in JS,im looking for a way to create a class or function,reusable everywhere in my code,just pass it parameters and get the result,because currently I am doing like this:
$.ajax({
dataType: "json",
type: "POST",
url: "#Url.Action("power","Ranking")",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "regionalManager": tmpString }),
success: function (result) {
})}
I write this every time I need, and im sick of it,
function sendAjaxCustom(DataType,Type,Url,Ctype,Data){
$.ajax({
dataType: DataType,
type: Type,
url: Url,
contentType: Ctype,
data: Data,
success: function (result) {
return result;
})}
}
You can call this function in JS like
var result = sendAjaxCustom("json","POST",'#Url.Action("power","Ranking")',"application/json; charset=utf-8",JSON.stringify({ "regionalManager": tmpString }));
you will have the result in result variable.
You can create a function like this
function ajax(url, data) {
$.ajax({
dataType: "json",
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
data: data,
success: function (result) {
})}
}
Pass the url if it's dynamic and the object data on the second parameter.
Just create a simple function with your variables that need to change between calls and return the $.ajax result from there.
function ajaxWrapper(url, data, callback) {
return $.ajax({
dataType: 'json',
type: 'POST',
url: url,
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data),
success: callback
});
}
When you want to call it:
ajaxWrapper('http://www.google.com/', { hello: 'world' }, function(result) {
console.log(result);
});
With the callback it's much more reusable, since you can use this anywhere and change what you do on completion of the function wherever you use it.
A simple solution is to return an object and pass it to the ajax and if some change is required then you can update the properties of the object before calling the ajax service
function commonAjaxParams() {
return {
dataType: "json",
type: "POST",
url: "#Url.Action("power","Ranking")",
contentType: "application/json; charset=utf-8",
//and so on that are common properties
}
}
//now in your application first call the function to get the common props
var commonParams = commonAjaxParams();
//change or add an parameter to your liking
commonParams.type = 'GET';
commonParams.success = function(){...} //if this action is need
commonPramss.error = function(){...}
//now call you ajax action
$.ajax(commonParams)
There is another way in which you may call the ajax function and you may get success, fail response return.
The benefit is you manage success or fail response independently for each ajax request.
$(document).ready(function() {
function ajaxRequest(dataType, requestMethod, dataURL, jsonData) {
return $.ajax({
dataType: dataType,
type: requestMethod,
url: dataURL,
contentType: "application/json; charset=utf-8",
data: JSON.stringify(jsonData)
});
}
var jsonData = {
"regionalManager": "jason bourne"
};
ajaxRequest(
"json",
"POST"
"#Url.Action('power','Ranking')",
jsonData)
.success((data) {
console.log("success");
}).error((err) {
console.log("error");
}).done(() {
console.log("done");
});
});
I have a problem with _fnReDraw in DataTables, I am using DataTables and that receive data of the my controller C#, my controller return json with Exception or sucess, but my DataTables ReDraw only a first situation.
For exemplo, if firs moment my controller return Exception my Datatables Redraw only exception, but se first moment return sucess my dataTables ReDraw only sucess.
$.ajax({
type: "POST",
url: '#Url.Action("Action", "Controller")',
data: JSON.stringify(json),
datatype: "JSON",
async: true,
contentType: "application/json; charset=utf-8",
success: function(result) {
eval($('#MydatatTables').dataTable())._fnDraw();
},
});
As is said in the comments, there doesn't seem to be any reason for you eval().
It is hard to understand exactly what you are saying in your question, but are you receiving table data back from your ajax call? If so, you need to repopulate the table with the new data before calling a table refresh. Using the new datatables api), you would do something like this:
$.ajax({
type: "POST",
url: '#Url.Action("Action", "Controller")',
data: JSON.stringify(json),
datatype: "JSON",
async: true,
contentType: "application/json; charset=utf-8",
success: function(result) {
var dtApi = $('#MydataTables').DataTable();
dtApi.clear(); //clear old data from table
dtApi.rows.add(result);
dtApi.draw();
}
});
If you do not need to update the data, you can just leave out the clear and rows.add. Hope this helps.
I have AJAX POST, the result is JSON:
$.ajax({
type: "POST",
url: "../../api/test",
data: JSON.stringify(source),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var upload = JSON.stringify(result);
console.log(upload);
}
});
The upload result is:
{"Link":0,"Title":"d","Description":"dada","Keywords":"dad"}
How can I get the value of Title?
Do not stringify the result, just use result.Title.
As you already have JSON string, It's simple as a pie!
All you need to do is to call the property you want from the variable you assigned your result to.
for example:
var post_response;
$.ajax({
type: "POST",
url: "../../api/test",
data: JSON.stringify(source),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
post_response = JSON.stringify(result);
console.log("Title: "+post_response.Title);
}
});
hope this helps.
I am facing a problem with the JQuery ajax function.
I am trying to send a simple json through the ajax asshown below.
$.ajax({
url: 'NewFile.jsp',
type: 'GET',
data: {"datasource":"hello1", definition:[{val1:"hello3"}]},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(msg) {
alert("Successfully posted "+JSON.stringify(json));
}
});
The problem is that when I do
System.out.println(request.getParameter("datasource"));
System.out.println(request.getParameter("definition"));
in my NewFile.jsp then I get hello1 for the first and null for the second.
Why I get null value in the second println()?
Thanks
In the url variable inside your ajax object, give the full URL of your request. Like so:
$.ajax({
url: 'www.google.com/',
type: 'GET',
data: {"datasource":"hello1", definition:[{val1:"hello3"}]},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(msg) {
alert("Successfully posted "+JSON.stringify(json));
}
});
Also, always make sure that there is a failure variable inside your object literal, so that you know it happened, but failed. Helps with debugging.