0

I am trying to pass two js array to jquery ajax to a php backend. However in the network tab, in the dev tools I receive > Failed to load response data. Any suggestions TY

var arr1 = [];
var arr2 = [];
var id = 2432;

$.ajax({
       type: "POST",
        url: '/api/put/update_dealer_manufacturer.php',
        async: true,
        data: {arr1, arr2, id},
        dataType: 'json',
        success: function(data) {
           let output = data;
        },
        error: function(data) {
          console.log("error");
          console.log(data);
        }
     });

In the backend I am doing:

$output = array(
  "success" => false,
  "msg" => ''
);

$arr1 = $_REQUEST['arr1'];
$arr2 = $_REQUEST['arr2'];
$id = $_REQUEST['id'];

On the back end I have

<?php

if(empty($_REQUEST['id'])){
   $output['msg'] = "Authentication error";
   echo json_encode($output);
   die();
}

$new_manufacture = $_REQUEST['arr1'];
$delete = $_REQUEST['arr2'];
$id = addslashes($_REQUEST['id']);

I loop then with a foreach on each arr. At the end of the backend file I do but for some reason I do not get any results as way to tell if I reached the backend file.

$output['success'] = true;
echo json_encode($output);
1
  • 1
    Probably some error on the backend. Look for it and then update your question with the error, stack trace and relevant code. Commented Sep 17, 2019 at 18:44

1 Answer 1

2

I can't comment (as rep doesnt allow for it.) Try changing to the following to see if it allows you to proceed.

data: {"arr1":arr1, "arr2":arr2, "id":id},
dataType: 'html',

I tested by doing a console.log for both success and fail..... this is what I get...

Array
(
    [0] => 123
    [1] => 234
)
Array
(
    [0] => 345
    [1] => 456
)
2432

Full code:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<script type="text/javascript">
var arr1 = ['123','234'];
var arr2 = ['345','456'];
var id = 2432;

$.ajax({
       type: "POST",
        url: 'code2.php',
        async: true,
        data: {"arr1":arr1, "arr2":arr2, "id":id},
        dataType: 'html',
        success: function(data) {
           console.log(data);
        },
        error: function(data) {
          console.log("error");
          console.log(data);
        }
     });

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

You will have to tweak to make it work with your code as your code is specific to what you need. The success just allows you to see whats happening. Based on the code you provided with my modifications, the array is being sent and it is returning in the console. You'll want to take that out for production once its working unless you absolutely need it for testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.