Advertise with us

Jquery AJAX - JSON or TEXT

Posted by  Goti Bandu
 2571  View(s)
Rate this:
Hi Developers,

I'm trying to retrieve the TEXT from the getsku javascript after submitting but not sure how to really do it. 

1) How do i retrieve the POST data?

2) How do i retrieve and post it back , if i have multiple varaible to pass back (Datatype:text)

3) When should i use JSON, and when text.

4) If i'm using JSON how do i read it(after javascript) and display it(returned data to javascript).

javascript in main page

 function getsku(){

    type: "POST",
    url: "funcAjax.php",
    data: { 'ddl1': $("#drop_1").val(), 'ddl2': $("#tier_two").val() },
    dataType: 'text', 
    success: function(data) {
    complete: function() {
        alert('Complete: Do something.');
    error: function() {
        alert('Error: Do something.');


<input type="button" value="Get SKU" onclick="getsku();" >

Trying to retrieve from another php and return a data to the above php(Having Issues here)

if(isset($_REQUEST['ddl1'])) { 
echo "FOUND1";
echo "FOUND2";

Thanks in advance for any recommendations or solutions.
  1. Re: Jquery AJAX - JSON or TEXT

    Always return JSON from your PHP. Then you can include as many variables as you need in your response, and an error code as well if appropriate - like this:

    {"error":"0","result1":"result 1 data","result2":"result 2 data"}
    Then your success function can become:

    success: function(data) {
        if (data.error != 0) {
            // An error occurred on server: do something 
        } else {
             // do something with data.result2
    Your PHP would become something like this:

    if(isset($_REQUEST['ddl1'])) { 
      echo json_encode(array("error"=>0, "result1"=>"FOUND1"));
      json_encode(array("error"=>1, "result1"=>"NotFOUND"));