Jump to content

AJAX and JSONP to do cross-domain calls with ProcessWire?


Recommended Posts

Posted

Is there anybody trying to use Ajax and JSONP in PW?

here is my code, but it does not working.  it looks like "$config->ajax" didn't work.

Client:

	$(function(){
		$.ajax({
				type: "GET",
				async: false,
				url: "http://localhost/processwire/",
				dataType: 'jsonp',
				jsonp: "callback",
				jsonpCallback: "jsonpCallback",
				success: function (data) {
					$.each(data,function(i,val){
						$("body").prepend(i + ' ' +val.fullName + ' ' + val.fullAdress + '<br>');
					});		
				},
				 error: function(){
					 alert('fail');
				 }			
		
		});
	});

Server:

if($config->ajax)
{
	$array = array(
		 '0' => array('fullName' => 'Meni Samet', 'fullAdress' => 'New York, NY'),
		 '1' => array('fullName' => 'Test 2', 'fullAdress' => 'Paris'),
	);
	if($input->get->callback)
	{
		header("Content-Type: application/json");
		echo $input->get->callback."(".json_encode($array).")";
	
	}
}
else
{
	include("./basic-page.php");
}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...