function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ButuzGOLButuzGOL 

VisualForce Ajax to Apex using jQuery

How can make ajax request using jQuery in VisulForce to Apex ?

 

Example

 $.ajax({

type :"POST",
url :'Apex-url',
dataType :"json",
success:function(data){
alert( data );
},
error :function(){
alert("Sorry, The requested property could not be found.");
}
});

JitendraJitendra

Hi,

 

You cannot invoke the Apex code using JQuery Ajax request. For this use Javascript Remoting.

 

If you want to invoke other VF page :

Lets say your Apex page name is "Test2" and you are calling it from Custom VF page itself. then

 

$.ajax({
	type :"POST",
	url :'Test2',
	dataType :"json",
	success:function(data){
		//This is your JSON Data
		alert( data );
	},
	error :function(){
	alert("Sorry, The requested property could not be found.");
	}
});

  In above code, it assumed that the Datatype of page is JSON.

 

Read below article to set the output format of visualforce page as JSON :

 

http://shivasoft.in/blog/salesforce/json-output-in-visualforce/

ButuzGOLButuzGOL

Thanks for answer 

So I have such request to apex page

$.ajax({
type :"POST",
url :'Test2',
dataType :"json",

data: {"one": "1", "two": 2},
success:function(data){
 alert( data );
},
error :function(){
alert("Sorry, The requested property could not be found.");
}
});

 

How can I get POST vars in apex  -> data: {"one": "1", "two": 2}, ?

 


 

 

Satyendra RawatSatyendra Rawat
You can try one more way it is very easy to handle success/Error response
.ajax({
     type :"POST",
     url :'Test2',
     dataType :"json",

     data: {"one": "1", "two": 2},
     success:successResponse,
     error :errorResponse
});

function successResponse(result)
{
    console.log("success =="+result);
}
function errorResponse()
{
    console.log("failed==");
}