"something went wrong parsererror" Ajax jquery Php

Published 1 month ago by filipe1221

Hi, iam using a form widht ajax and was working but now gives error "something went wrong parsererror SyntaxError: Unexpected end of JSON input" but return 200 var form_data = $('.form').serialize() ;

    event.preventDefault(e);

    $.ajax({

        type: 'POST',

        url: 'ajax_form?type='+type,

        data:form_data,

        dataType: 'json',

        success: function(response){

            console.log(response);

        },

        error: function( req, status, err ) {

            console.log( 'something went wrong', status, err );

        }

    })

if i print my return in php returns a array ['class']='success' ['code']="ok"

i try delete dataType: 'json', and use json encode in return but nothing

Best Answer (As Selected By filipe1221)
filipe1221

Got it

public function define_action(Request $request){

$this->store_edit($request);

}

should be

public function define_action(Request $request){

return $this->store_edit($request);

}

jlrdw
jlrdw
1 month ago (227,210 XP)

was working but now gives error "something went wrong parsererror

What did you change? What is the controller code.

Cronix
Cronix
1 month ago (571,710 XP)
console.log( 'something went wrong', status, err );

You said you got the "something went wrong" message, but what was the contents of status and err?

Do you have any related errors in the laravel error log?

filipe1221

my controller is just a query and return and array php width 2 keys (class, code) if i print array is correct is just the response failling

@cronix something went wrong parsererror SyntaxError: Unexpected end of JSON input at parse () at Ut (jquery.min.js:2) at k (jquery.min.js:2) at XMLHttpRequest. (jquery.min.js:2)

Cronix
Cronix
1 month ago (571,710 XP)

Well we're not going to be able to tell what was wrong from just the javascript.

Your url should always start with a http, or just a /, otherwise it's a relative request and not absolute. Like if the page you were triggering that code was on http://yoursite.com/some/segment, the ajax request would go to http://yoursite.com/some/segment/ajax_form when you probably want it to go to http://yoursite.com/ajax_form

url: '/ajax_form?type='+type,

This would be much better if it was using an id instead of a class to target the specific form.

var form_data = $('.form').serialize() ;

Do you have any related errors in the laravel error log?

?

filipe1221

how can i see laravel error log? sry :)

Cronix
Cronix
1 month ago (571,710 XP)

/storage/logs/laravel.log

I'd first delete the laravel.log file, and then rerun the request so the error log will only have errors pertaining to that specific request.

filipe1221

Thanks. I Delete log file, and rerun again the request but there's no file now, it meens is not generating erros by laravel i think

Cronix
Cronix
1 month ago (571,710 XP)

And what does the request/response tabs in the network tab in your browsers developer tools tell you is happening for that request?

At some point, you'll need to show the controller so we can see how it's returning the data back to the ajax request.

jlrdw
jlrdw
1 month ago (227,210 XP)

You still said it was working then suddenly not working what did you do different, a code change or do you mean working on local and not production.

filipe1221

i know now Before that i was going directly to a function to save changes in bd...

public function store_edit(Request $request)

{

}

//but now i was doing like this

public function define_action(Request $request)

{

if (isset($_REQUEST['id'])){

$this->store_edit($request);

}else{

$this->insert_new($request);

}

}

and was that if i go directly to function who returns the response works but if i use other function before nothing is returned, so ajax response is not a problem but probably my request

Cronix
Cronix
1 month ago (571,710 XP)

So do both of your store_edit() and insert_new() methods return json? The problem is probably due to whatever you're returning back to the ajax request as the response.

It's hard to troubleshoot this because you're only giving us a few pieces of the puzzle and we need the whole picture.

Show the controller code, and all relevant methods that it's using for this request. Show the view with the form. Show your route that it's using.

filipe1221

ok that will stay extense file.js

var form_data = $('.form').serialize() ;

    event.preventDefault(e);

    $.ajax({

        type: 'POST',

        url: 'ajax_form?type='+type,

        data:form_data,

        dataType: 'json',

        success: function(response){

            notify(response);

            if(response.empty_fields>0){

                $.each(response.field, function (i) {

                    $.each(response.field[i], function (key, val) {

                        if(key=='field'){

                            $("#"+val).css({"border": "1px solid red", "opacity": "0.5"});

                        }
                    });

                });
            }

        },

        error: function( req, status, err ) {
            console.log( 'something went wrong', status, err );
        }

    })

//now controller

public function define_action(Request $request){

    $data=$_REQUEST;

    $request->merge($data);

    $this->store_edit($request);

}

public function store_edit(Request $request)

{

    $page_id= $request->get("id");

    $this->page = new Admin\Pages();

    $page_update= $this->page->find($page_id);


    if($return['total_errors']==0){

        $array_update=prepare_store_multilingual_form($request, $fields);

        $update=$page_update->update($array_update->all());

        if(!$update){

            $return['message'] = "Error";

            $return['class']='danger';

            $return['success']='denied';

        }else{


            $return['message'] = "Updated";

            $return['class']='success';

            $return['success']='ok';

        }

    }else{
        $return['message'] = "Error";


        $return['class']='danger';

        $return['success']='denied';

    }

    return $return;
}

now if my request via ajax was directly to store_edit works but if i need pass by other function before like "Define action" this not work

Cronix
Cronix
1 month ago (571,710 XP)
$data=$_REQUEST;
$request->merge($data);

You do realize that $request already contains everything that $_REQUEST holds? So you're duplicating everything in the array. Why are you even using $_REQUEST instead of just using $request?

if($return['total_errors']==0){

$return hasn't been defined yet

prepare_store_multilingual_form($request, $fields);

What is that function?

filipe1221

yeh sry thats my last try should be like this like this

public function define_action(Request $request){ $this->store_edit($request); }

return was not necessary because iam using before $return['total_errors']=0 just to pass here

prepare_store_multilingual_form($request, $fields); its a function to update some fileds from my form in db, and was updating correct

the point is everithing is going well all updates everything

filipe1221

Got it

public function define_action(Request $request){

$this->store_edit($request);

}

should be

public function define_action(Request $request){

return $this->store_edit($request);

}

Please sign in or create an account to participate in this conversation.