0

I want to send to the backend a json string as one parameter and also other parameters. I am expecting to receive results as jsonp.

the parameters are

$param1 = '                          {
                              "MetaData": [
                                    {"Index": "0", "Name": "COLUMN_NAME"},
                                    {"Index": "1", "Name": "VALUE"},
                                    {"Index": "2", "Name": "VALUE_CHANGED"}
                                            ],
                              "Data": [
                                    ["ORDER_NO","*2733","f"],
                                    ["DISCOUNT_NO","1","f"],
                                    ["DISCOUNT_TYPE","S1","f"],
                                    ["DISCOUNT","11.4","t"],

                                            ]
                            } ';

how should I write the syntax in js and how should I receive it from the php side? when I try to use it like below it doesn't work.

$.ajax({

    url: 'http://... myfile.php',
    data: {PARAMETER1: $param1, PARAMETER2: $param2}
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    error: function() { alert('Failed!'); }

}).done(function( data ) {
                            $.each(data, function(k,v) {
                               alert( "key: "+k+"  val:" + v);
                            })
                         });
3
  • why you want to sent data to server as json format ? Commented Apr 7, 2014 at 20:49
  • Why are you sending data in this way? Why not just make $param1 an object, and let it be decoded into $_GET automatically? Commented Apr 7, 2014 at 21:05
  • Msi Saurovh I need to send multiple rows to the server side. so it must be an array or json or similar Commented Apr 7, 2014 at 21:49

3 Answers 3

1

Your Javascript code looks OK to me. On the PHP side, it would be:

$param1 = json_decode($_GET['PARAMETER1'], true);

Then you can access $param1['MetaData'][$i]['Index'], for instance.

Sign up to request clarification or add additional context in comments.

Comments

1

you are sending object containing two strings

at your php code you should decode them to json using json_decode

it depends on your back end code / framework ... just read them then decode them

Comments

0
$(document).ready(function(){


        param1 = ' { "MetaData": [ {"Index": "0", "Name": "COLUMN_NAME"},
        {"Index": "1", "Name":"VALUE"}, {"Index": "2", "Name": "VALUE_CHANGED"} ], 
        "Data": [ ["ORDER_NO","*2733","f"], ["DISCOUNT_NO","1","f"],
        ["DISCOUNT_TYPE","S1","f"],
        ["DISCOUNT","11.4","t"], ] } '; 
         param2 ='2';

         $.ajax({ url: 'ajaxresponse.jsp', 
         type: 'POST',
         data:'{"PARAMETER1":'+ param1+',"PARAMETER2":'+ param2+'}',
         contentType: 'application/json; charset=utf-8',
         dataType: 'json',
         async: false, 
         success: function(msg) { alert(msg); } });

    }); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.