Chidi Okeh
Chidi Okeh

Reputation: 1557

Still struggling to pass JSON object along with a url param to a php file.Any ideas how?

  // Creating variables to hold data from textboxes. First building associated details
  myData.Token = $("#token").val();
  myData.BuildingDisplay = $("#BuildingDisplay").val();
  myData.FeatureID = $("#FeatureID").val();
  myData.BuildingID = $("#BuildingID").val();
  myData.Address = $("#Address").val();
  myData.City = $("#City").val();
  myData.District = $("#District").val();
  myData.Location = $("#Location").val();
  myData.State = $("#State").val();
  myData.StreetName = $("#StreetName").val();
  myData.Zip = $("#Zip").val();
  myData.X = $("#X").val();
  myData.Y = $("#Y").val();
$.ajax({
  type: "POST",
  url: "proxyCheck.php",
  data: "{ 'Token': '" + mytoken + "','Address': '" + myaddress + "', 'City': '" + mycity + "','Location': '" + mylocation + "','State': '" + mystate + "', 'StreetName': '" + mystreetname + "', 'Zip': '" + myzip + "', 'X': '" + myX + "', 'Y': '" + myY + "'}",
  contentType: "application/json;charset=utf-8",
  dataType: "json",
  async: false,
  success: function (response) {
    alert("Record has been added successfully.");
    window.location.reload();
  }
});

When above data is formatted into JSON object, it is presented in the following format:

{data: 
      {
        "token":"73264280-be3f-4f5b",
        "BuildingDisplay":"Fire Station 21 Fairburn - 19 East Broad St.",
        "CallerEmail":"[email protected]",
        "CallerFax":"",
        "CallerFirstName":"Jim",
        "CallerLastName":"Parker",
        "CallerMiddleInitial":"",
        "CallerOtherPhone":"",
        "CallerState":"",
        "CallerWorkPhone":"918-354-2874"}}

What I would like to do is pass them to proxyCheck.php in the following:

 proxyCheck.php?data={data: 
      {
        "token":"73264280-be3f-4f5b",
        "BuildingDisplay":"Fire Station 21 Fairburn - 19 East Broad St.",
        "CallerEmail":"[email protected]",
        "CallerFax":"",
        "CallerFirstName":"Jim",
        "CallerLastName":"Parker",
        "CallerMiddleInitial":"",
        "CallerOtherPhone":"",
        "CallerState":"",
        "CallerWorkPhone":"918-354-2874"}}&token=73264280-be3f-4f5b

Please notice &token=73264280-be3f-4f5b

I can't seem to figure out how to do this.

Can someone please help?

$.ajax({
    type: "POST",
    url: "proxyCheck.php?token=" + myData.Token,
     data: JSON.stringify(myData)
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    async: false,
    success: function (response) {
        alert("Record has been added successfully.");
        window.location.reload();
    }
});
  return false;
 }

//proxyCheck.php

<?php
  $ch = curl_init("http://domain/UserServices/Create");
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt ($ch, CURLOPT_POSTFIELDS, $_POST);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $output = curl_exec($ch);
  curl_close($ch);
  echo $output
 ?>

//*******************

//AJAX

var uname = $("#user").val();
var upass = $("#pass").val();

$.post("proxyValidate.php",
  { data: JSON.stringify({ LoginName: uname,Password: upass }) })
    .done(function(data) {
        var result = JSON.parse(data);
        switch(result.Status) {
            case 0:
                //login successful
                tokenVal = result.Value.Token;
                location.href = "http://domain/userService.php?token="+tokenVal;
                break;

            case 2:
                //invalid login
                alert(result.Message);
                break;
        }
    })
    .fail(function() {
        alert("The AJAX request failed!");
    });
  });

//proxyValidate

  <?php

    $ch = curl_init("http://domain/Validation/Validate");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $_POST);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    echo $output
  ?>

Upvotes: 0

Views: 503

Answers (3)

Chidi Okeh
Chidi Okeh

Reputation: 1557

For anyone who may run into similar problem as me, here is the solution that worked for me:

$.ajax({
    type: "POST",
    url: "proxyCheck.php?token",
    data: {
        data: JSON.stringify(myData),
        token: myData.Token
    },
    dataType: "json",
    async: false,
    success: function (response) {
        alert("Record has been added successfully.");
        window.location.reload();
    }
});

Upvotes: 1

dkasipovic
dkasipovic

Reputation: 6120

You really should use POST for that kind of data. If you insist on using GET as you suggested, you should urlencode the JSON, but still, it could easily happen that you url gets too long an thus unable to be open.

EDIT: A solution that should work, by @Archer

$.ajax({
    type: "POST",
    url: "proxyCheck.php?token=" + myData.Token,
    data: {
        data: JSON.stringify(myData)
    },
    dataType: "json",
    async: false,
    success: function (response) {
        alert("Record has been added successfully.");
        window.location.reload();
    }
});

Upvotes: -1

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

There's no need to convert the javascript object to a string - jQuery will do this for you. Try this...

$.ajax({
    type: "POST",
    url: "proxyCheck.php?token=" + myData.Token,
    data: myData,
    dataType: "json",
    async: false,
    success: function (response) {
        alert("Record has been added successfully.");
        window.location.reload();
    }
});

There's a slight issue with variable names being case sensitive (token and Token, for example), but the above code should post what you need, as well as have the token value in the URL.

Alternatively, if you need to pass the data as a string then you can do the following...

$.ajax({
    type: "POST",
    url: "proxyCheck.php?token=" + myData.Token,
    data: {
        data: JSON.stringify(myData)
    },
    dataType: "json",
    async: false,
    success: function (response) {
        alert("Record has been added successfully.");
        window.location.reload();
    }
});

Upvotes: 2

Related Questions