weixin_33712987 2016-02-16 20:20 采纳率: 0%
浏览 28

使用AJAX将JSON发送到PHP

Good afternoon,

I am trying to send some values from a text form (made by Gravity Form) to a PHP file where I can then send those values to an API (Insightly). The reason for this is Insightly does not take AJAX calls.

The code I have is below, when I click the submit button I keep getting the "Failed To Send" error defined below, it never succeeds. So far I haven't finished my PHP scripting, I only have the following line:

$obj = $_POST['myData'];

I've been researching this issue for awhile now and still can't figure it out. Any assistance is greatly appreciated! Without further ado, here is the Javascript I have so far:

$(document).ready(function() {
$("#gform_submit_button_1").click(function() {
    var name = document.getElementById("input_1_19").value;
    // split name into first and last names
    var fname = name.split(" ")[0];
    var lname = name.split(" ")[1];
    var company = document.getElementById("input_1_15").value;
    var email = document.getElementById("input_1_6").value;
    var phone = document.getElementById("input_1_14").value;
    var hearAbout = document.getElementById("input_1_10").value;
    var solution = document.getElementById("input_1_11").value;
    var market = document.getElementById("input_1_17").value;
    var details = document.getElementById("input_1_5").value;

    var information =
    {
        "fname": fname,
        "lname": lname,
        "company":company,
        "email": email,
        "phone": phone,
        "hearAbout": hearAbout,
        "solution": solution,
        "market": market,
        "details": details
    };

var dataString = JSON.stringify(information);

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "insightlyAPI.php",
        data: {myData: dataString},
        success: function(data){
            alert('AJAX Success!');
        },
        error: function(){
            alert('Failed To Send');
        }
    });
});
});
  • 写回答

1条回答 默认 最新

  • YaoRaoLov 2016-02-16 21:10
    关注

    The dataType parameter you pass to $.ajax call tell jQuery to expect data from the server:

    "json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown.

    Try to return json from your PHP script, for example, add at the end:

    echo json_encode(array());
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧