-1

Currently my project need me to send file via ajax. I has been search from online and follow the step. Here is the result that I following. It really work perfectly of send file to server site but have issue of return json. I have been try echo string,int and json. Only json not returning.

I also tried remove the contentType:false, it really able return the json but not passing the file to backend.

from.php

var country_code = $('#country_code').val();
        var operator = $('#operator').val();
        //var upload_file = $('#upload_file')[0].files[0].name;
            var upload_file_info = $("#upload_file").prop('files')[0];
        console.log($);
        var frmData = new FormData();
        var action = $("#frmImport").attr('action');
            frmData.append('upload_file', upload_file_info);
            frmData.append('operator', operator);
            frmData.append('country_code', country_code);

        $.ajax({
            type: 'post',
            data: frmData,
            url: action,
            dataType: 'json',
            cache: false,
                    contentType: false,
                    processData: false,
                    timeout: 30000,
            success:
                function(data) {
                    if(data['status'] == 1)
                    {
                        var txt;
                        var r = confirm(data['message']);
                        if (r == true) {
                            location.href = '/operator/importoperatorinfo?country_code='+data['upload_info']['country_code']+'&operator_id='+data['upload_info']['operator_id']+'&filename='+data['upload_info']['new_filename'];
                        } else {
                            return false;
                        }
                    }
                    else
                    {
                        alert(data['message']);
                        $('#upload_file').focus(); 
                        return false;
                    }
                },
                    error:
                        function(error,data)
                        {
                            console.log(data);
                        }
        });

process.php

 <?php

    echo json_encode(array('status'=>200));
?>
Community
  • 1
  • 1
d3no
  • 121
  • 1
  • 3
  • 12
  • how are you accessing the json in php? – Exprator May 16 '17 at 12:15
  • The data pass to php are not json – d3no May 16 '17 at 12:16
  • 1
    What does console says? What is the url you are calling with ajax? Have you tried `console.log(data)` in success function? – cakan May 16 '17 at 12:17
  • can you show the output of the json – Exprator May 16 '17 at 12:18
  • console.log(data); show parseerror. – d3no May 16 '17 at 12:21
  • "I also tried remove the contentType:false". ContentType is related to the data being _sent_. "dataType" is related to the data being _received_. Anyway, you can check your network tab to see what the response data looks like, and/or write `console.log(JSON.stringify(data));` in the first line of the "success" callback to see a visual representation of the returned data. – ADyson May 16 '17 at 12:21
  • "show parseerror"...what exactly does the error message say, please? Again, you can also check the response in the network tab anyway. – ADyson May 16 '17 at 12:22
  • It showing empty at network response. I had try print both json and string. The json juz invisible in the response. – d3no May 16 '17 at 14:47
  • are you absolutely certain that the right bit of "process.php" that generates the JSON is being called? It sounds like maybe it's not hitting that code. You need to debug the PHP I think. – ADyson May 16 '17 at 19:41
  • The echo json are not in any of condition. It place on the end of the code. I had try echo string at next line of the of the json. It can return to ajax, just the json missing in the return. – d3no May 17 '17 at 01:55
  • _something_ must be going wrong. If what you say is true there is no reason not to get the response back. That means that there is a problem somewhere. Is the ajax call definitely returning a HTTP 200 response (I don't mean your own code creating a "status" variable in the JSON, I mean the actual HTTP response header)? You are certain there's no error in the PHP causing it not to complete the script? You are 100% certain that the line in process.php is actually called? Like I said, you need to debug the PHP. – ADyson May 17 '17 at 08:58

1 Answers1

-1

In ajax success function, you need to parse your json output first.

var data = JSON.parse(data);

Then use data.status to access your output as output is an object and you are using array syntax.

Bhavik
  • 495
  • 2
  • 10
  • setting `dataType: "json"` as OP has done makes this step unnecessary. You can read this at http://api.jquery.com/jquery.ajax/ – ADyson May 16 '17 at 12:20