javascript - Why my ajax request always throws error? -


here code:

php:

public function export(request $request){      $file = "export.txt";     if(isset($_post["type"])){         file_put_contents("$file",$_post["text"]);     }     if (file_exists($file)) {         header('content-description: file transfer');         header('content-type: text/plain'); // appropriate header type txt file         header('content-disposition: attachment; filename="'.basename($file).'"');         header('expires: 0');         header('cache-control: must-revalidate');         header('pragma: public');         header('content-length: ' . filesize($file));         readfile($file);         exit;     } } 

js:

    $(document).on('click', '#export', function () {         var names =  ['علی','فرید'];         var namess = names.join('\n');         $.ajax({             type: "post",             url: "/export",             headers: {                 'x-csrf-token': $('meta[name="csrf-token"]').attr('content')             },             data: {                 type: "save",                 text: namess             },             datatype: "json",             success: function(){                 var href = '/export?filename=export.txt';                 window.location = href;             },             error: function(){                 alert('wrong');             }         });     }) 

always error part executes. mean alert wrong. how can fix it? i'm trying making .txt download file.

noted when run path: http://localhost:8000/export?filename=export.txt .. export.txt downloaded.

you can download using code:

window.location="export?filename=export.txt"; 

if want post data :

   $('<form action="comments.php" method="post"/>')         .append($('<input type="hidden" name="type" value="save">'))        .append($('<input type="hidden" name="text" value="'+ namess +'">'))         .appendto($(document.body)) //it has added somewhere <body>         .submit(); 

full code:

 $(document).on('click', '#export', function () {      var names =  ['علی','فرید'];     var namess = names.join('\n');       $('<form action="export?filename=export.txt" method="post"/>')         .append($('<input type="hidden" name="type" value="save">'))        .append($('<input type="hidden" name="text" value="'+ namess +'">'))         .appendto($(document.body)) //it has added somewhere <body>         .submit();      });  }); 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -