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 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题