leoric Posted January 26, 2015 Posted January 26, 2015 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"); }
Jan Romero Posted January 26, 2015 Posted January 26, 2015 $config->ajax uses $_SERVER['HTTP_X_REQUESTED_WITH'], which isn’t sent with JSONP requests. You can just check for $input->get->callback on the server side to determine if it’s a JSONP request. 5
leoric Posted January 27, 2015 Author Posted January 27, 2015 $config->ajax uses $_SERVER['HTTP_X_REQUESTED_WITH'], which isn’t sent with JSONP requests. You can just check for $input->get->callback on the server side to determine if it’s a JSONP request. thank you,Jan , you solved my problem.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now