0

I'm trying to use ajax call for my laravel app but I'm getting one extra slash even its back slash or forward slash. Can any one tell me why it is happening and what is the solution cause its waste my three 3 hours in a row. here is my ajax.

$(document).ready(function(){

            $('#status').change(function() {

           var status=$('#status').val()
           alert(status);
                $.ajax({
                type:'GET',
                url:'{{route('prescription_status')}}',
                data:{status:status},
                datatype:"json",
                success:function(data){
                        alert(data);
                    $('.filebody').html(data);
                }
            });
        });
        });

here is my controller.

public function getStatus(Request $request)
    {

        $files  = Fileupload::where('status',$request->status)->get();
        $users=User::all();
        $output='';
        foreach($files as $file) {
            $output .= '<tr>';
            foreach ($users as $user) {
                if ($user->id == $file->patient_id) {
                    $output .= "<td >" . $user->first_name . " " . $user->last_name."</td>" ;
                }
            }
            foreach ($users as $user) {
                if ($user->id == $file->user_id) {
                    $output .= "<td >" .$user->first_name . "" . $user->last_name."</td>" ;
                }
              }
            $output.="<td>".$file->filename."</td> <td>".$file->status."</td><td><a href='".route('download-file',[$file->id])."'>Download</a></td>";

            $output .="</tr>";
        }


//        $data=array(['output'=>$output]);
        echo json_encode($output) ;
    }

output which I'm getting.

<tr><td >Ali Test<\/td><td >HuzailJamil<\/td><td>353fc620-742f-11ea-a199-fd883b1807d3_2020-04-01 03:41:09pm.pdf<\/td> <td>processed<\/td><td><a href='https:\/\/www.medeconsult.com.au\/upload\/file\/download\/1'>Download<\/a><\/td><\/tr><tr><td >Huzail Jamil<\/td><td >HuzailJamil<\/td><td>754326f0-74e8-11ea-b110-23d0ecfe041c_2020-04-02 01:47:13pm.pdf<\/td> <td>processed<\/td><td><a href='https:\/\/www.medeconsult.com.au\/upload\/file\/download\/7'>Download<\/a><\/td><\/tr>

1 Answers1

1

This is happening because, in some contexts, an unescaped / can cause problems.

Since, in JSON, "\/" and "/" are equivalent, it isn't a problem. The escape character will go away when the JSON is parsed.

Your problem is that your JavaScript is trying to treat the JSON as HTML and not parsing it as JSON.

There are two reasons for this:

Content-Type

By default, PHP claims to output text/html but you are outputting JSON and didn't change the default.

header("Content-Type: application/json")

Overrides

You can override the Content-Type from the client by specifying the dataType.

You tried to do this, but you failed to use a capital T. JavaScript is case sensitive.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335