Note that there are some explanatory texts on larger screens.

plurals
  1. POPOST a JSON object from Jquery to PHP and decoding it (cross domain)
    primarykey
    data
    text
    <p>I have code working to pass JSON objects from Jquery to PHP page.</p> <p>The problem is with sending Cross-domain requests, If i try </p> <pre><code>dataType:'json' </code></pre> <p>in jquery, it gives me a xhttp error (the security) error which I understand. </p> <p>I also understood after reading <a href="https://stackoverflow.com/questions/2943103/jsonp-implications-with-true-rest">this post</a> that JSONP only works for GET methods </p> <p>This is how I am creating and using my object:</p> <pre><code>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); } ); </code></pre> <p>What is the solution for me to pass a JSON object cross domain? if that is not possible, what is an alternate solution ?</p> <p>Please advise</p> <p>Thanks</p> <p>Edit:</p> <p><strong>Jquery code</strong></p> <pre><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); } }); </code></pre> <p>**</p> <blockquote> <p>The error on PHP end is: Invalid argument supplied for foreach() in ....</p> </blockquote> <p>** </p> <p><strong>PHP Code (simplified version)</strong></p> <pre><code>&lt;?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")). ')'; } </code></pre> <p>?></p> <p>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. </p> <p>real sorry about that</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload