最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get data in PHP from AJAX jQuery request - Stack Overflow

programmeradmin2浏览0评论

So I've followed all your advice about making an jQuery or Javascript AJAX request to a php file on a server.

The problem I'm running in to is that my response is this

Fatal error: Array callback has to contain indices 0 and 1 in

So here is my jQuery

$(document).ready(function(){

    $(".ex .select").click(function(){
    $("body").data("id", $(this).parent().next().text());

Those lines serve to capture a variable and save it as data on the body element, so I can use it later.

But I want to send that same variable to PHP file, to add to a plex API request - eventually to return the results, create $_SESSION variables and so forth

    var pageId = {
        id: $("body").data("id"),
    };

But let's avoid that as a problem and just make my array simple like

var pageId = {
        id: "12345",
    };

    $.ajax({
        type: 'GET',
        url: 'test.php',
        data: pageId,
        datatype: 'json',
        cache: false,
        success: function (data, status) {
            alert("Data: " + data + "\nStatus: " + status);
        }
    });
});
});

And for now at least I was hoping to keep my PHP file simple for testing my grasp of the concepts here

<?php

$id = $_GET('id');

echo json_encode($id);

?>

I'm expecting the response

Data: 12345

Status: Success

But I get the Error Message above.

So I've followed all your advice about making an jQuery or Javascript AJAX request to a php file on a server.

The problem I'm running in to is that my response is this

Fatal error: Array callback has to contain indices 0 and 1 in

So here is my jQuery

$(document).ready(function(){

    $(".ex .select").click(function(){
    $("body").data("id", $(this).parent().next().text());

Those lines serve to capture a variable and save it as data on the body element, so I can use it later.

But I want to send that same variable to PHP file, to add to a plex API request - eventually to return the results, create $_SESSION variables and so forth

    var pageId = {
        id: $("body").data("id"),
    };

But let's avoid that as a problem and just make my array simple like

var pageId = {
        id: "12345",
    };

    $.ajax({
        type: 'GET',
        url: 'test.php',
        data: pageId,
        datatype: 'json',
        cache: false,
        success: function (data, status) {
            alert("Data: " + data + "\nStatus: " + status);
        }
    });
});
});

And for now at least I was hoping to keep my PHP file simple for testing my grasp of the concepts here

<?php

$id = $_GET('id');

echo json_encode($id);

?>

I'm expecting the response

Data: 12345

Status: Success

But I get the Error Message above.

Share Improve this question asked Dec 25, 2017 at 4:26 Mark SlaterMark Slater 1811 gold badge2 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You need to change $_GET('id') to $_GET['id'] and in order to send status, you need to add status to an array just like below and try to parse JSON on your response.

<?php

    $id = $_GET['id'];
    $result = array("id"=>$id, "Status"=>"Success");

    echo json_encode($result);

?>

And access in jquery using . like if you use data variable in success then

success:function(data){
    alert(data.Status);
}

change $id = $_GET('id'); to $id = $_GET['id'];

because $_GET, not a function it's an array type element.

发布评论

评论列表(0)

  1. 暂无评论