0

I have code working to pass JSON objects from Jquery to PHP page.

The problem is with sending Cross-domain requests, If i try

dataType:'json'

in jquery, it gives me a xhttp error (the security) error which I understand.

I also understood after reading this post that JSONP only works for GET methods

This is how I am creating and using my object:

function order(id, name) {

        return {
            id: id,
            name: name
        }

    }

    var orders= [];

    orders.push(order("123", "Lamb Kebab"), product("234", "Chicken"));

    var jsonOrders = $.toJSON(orders); 

    $.post(
        "process.php",
        {orders: jsonOrders },
        function(data){
            $("#result").html(data);
        }
    );

What is the solution for me to pass a JSON object cross domain? if that is not possible, what is an alternate solution ?

Please advise

Thanks

Edit:

Jquery code

 function product(code, type) {

        return {
            code: code,
            type: type
        }

    }

    var products = [];

    products.push(product("333", "Product one"), product("444", "Second product"));

    var jsonProducts = $.toJSON(products); 

    $.ajax({
        type: "GET",
        url: "http://page.tld/foo.php",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        data:JSON.stringify({products: jsonProducts}),

        error: function (msg) {
            //alert("error");
            console.log("Error here" +msg);         

        },


        success: function (msg) {
                console.log("Success"+msg);
        }
    });

**

The error on PHP end is: Invalid argument supplied for foreach() in ....

**

PHP Code (simplified version)

<?php header("Content-type: application/json; charset=utf-8");
require_once('json.php');

if (isset($_GET['products'])) {

$products = json_decode($_GET["products"],"true");
foreach ($products as $product){
echo $_GET['callback'] . '(' .(json_encode($product["type"])). ')';
}
}
else
{
echo $_GET['callback'] . '(' .(json_encode("not found")). ')';
}

?>

it is going into the block where it is able to find $_GET['products'], is it a parsing error on my part? i am sure it is an obvious mistake but im not able to spot it.

real sorry about that

Community
  • 1
  • 1
Mponnada
  • 263
  • 5
  • 18
  • Just pass it using jsonp get (maybe base64 encoded) as a get param, then decode it back on the server – yent Aug 08 '12 at 09:28
  • wow, is it really that simple? sure, let me check.. – Mponnada Aug 08 '12 at 09:31
  • im passing data as data:JSON.stringify({products: jsonProducts}), on php end, Im checking for if (isset($_POST['products'])) ... .. is that the right way to go? – Mponnada Aug 08 '12 at 09:48
  • If you use GET you'd better check $_GET['products'] instead of $_POST ... – yent Aug 08 '12 at 09:49
  • sorry, my mistake, I am using, $_GET['products']; it is not able to find it; i am editing the question with PHP code as well as the server response.. hope you will get a better idea where im going so wrong.. also, found this [link](http://stackoverflow.com/questions/2799304/sending-jsonjquery-to-php-and-decoding-it) but it only talks about posting strings, and not JSON objects. – Mponnada Aug 08 '12 at 09:54
  • You should use a tool like firebug to check how does the sent request looks, btw using JSON.stringify turns your json into a string so you really are passing a string, no worries, also since jquery proccess data passed to the ajax method you should build your url like 'http://domain.tld/foo.php?products=' + JSON.stringify(jsonProducts), that may be better – yent Aug 08 '12 at 10:00
  • wow, great advice, let me incorporate it straight away, I have also supplied my entire code.. – Mponnada Aug 08 '12 at 10:12
  • print_r or var_dump your $products before your foreach to check its type. – yent Aug 08 '12 at 10:17
  • var_dump($products) is giving me a NULL and print_r($products) is returning true – Mponnada Aug 08 '12 at 10:23
  • url sent looks like this: http://domain.tld/foo.php?products=[{"code":"333","type":"Product one"},{"code":"444","type":"Second product"}] – Mponnada Aug 08 '12 at 11:21
  • 1
    That's good, but I think you shoul throw some base64 encoding here (or at least EncodeURIComponent) to deal with funny characters in names. If you var_dump $_GET what do you see ? The problem may com from the data passed to json_decode not beeing correct json because of some reason. – yent Aug 08 '12 at 11:32

1 Answers1

0

I used the GET parameter and decoded JSON on the PHP end.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Mponnada
  • 263
  • 5
  • 18