| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Tuesday, November 9, 2010

Cross-Origin Javascripting with PHP

I had an awesome moment of WIN today when my boss asked me if we could add a drop-down selector to a SharePoint form that was populated with information from an SQL database. I have already set up my LAMP server to talk to the SQL database so I figured the easiest way to get this done would be to have an Ajax request sent to my LAMP server and forward that on to the SQL server and return the results to the SharePoint form.

Simple right?

Except that my LAMP server is on a different domain than the SharePoint server so that would classify as Cross-Origin and my Ajax would fail.

After a bit of research, I found that I can manipulate the headers sent out by the PHP pages served by my LAMP server to allow for Cross-Origin scripting!

Here's how:
http://LAMP/sql.php
<?php

header('Access-Control-Allow-Origin: http://sharepoint/');
...
# more PHP to query the SQL server & build the resulting page
...
http://SharePoint/List/NewForm.aspx (in CEWP)
<script src='jquery.js'></script>
<script type='text/javascript'>
...
$('#drop-down_id').load('http://LAMP/sql.php');
...
</script>

0 comments: