duanbiao4025 2014-02-26 14:05
浏览 45
已采纳

仍在努力将JSON对象与url param一起传递给php文件。任何想法如何?

  // 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":"Jim.Parker@yahoo.com",
        "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":"Jim.Parker@yahoo.com",
        "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
  ?>
  • 写回答

3条回答 默认 最新

  • dongxia527680 2014-02-28 19:35
    关注

    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();
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化