Monday, October 27, 2014

Step By Step Cross Domain Pass Parameter

Objective of this tutorial is helping those who want to create api but having the issue of cross domain problem.

How to Create Client Call API
Step 1:
Create javascript file name myjsfile.js and paste the following codes.

  $('#btn_name').on('click', function(e){
      variable1: "maybe textbox value",
      variable2: "maybe textbox value",
      variable3: "maybe textbox value"
    function(data) {
        var str = data['result'];

Step 2:
Include the following code before </head> tag.

<script src="js/myjsfile.js" type="text/javascript"></script>

You are now able to pass parameter to function from difference domain  .

How to Create Server Response
Step 1:
Create php file name myapifile.php and paste the following codes.

 header("Content-Type: application/json, charset: utf-8;");
 $arr['result'] = "Here is your result data";
 echo $_GET['jsoncallback'].'('.json_encode($arr).');';


You may get Uncaught SyntaxError: Unexpected token if you did not include $_GET['jsoncallback'].


No comments:

Post a Comment