0

So basically i am trying to build a survey with AJAX in Laravel. But when I try to submit it, in console i see this error "baseUrl not defined". Bear in mind that my jscript is external. Although I can put it inside the blade if needed.

Here is my JScript code

$(document).ready(function(){
$('#submitanketa').click(function(){
var uneto = $('input[name="radio"]:checked').val();
var token = $('#token').val();
$.ajax({
method: "POST",
url: baseUrl + "/post/post/ajax",
data: {
unos: uneto,
_token: token
},
success: function (data, xhr) {
/* location.reload();*/
console.log();
console.log(xhr);
$('#form1').html("<h1 class='hanketa'>Hvala na glasanju!</h1>");
},
error: function(xhr, status, error){
console.log(xhr);
console.log(status);
console.log(error);
        }
    });
});

});

Here is my view with a survey

<fieldset>
    <div id="form1">
        <legend class='legenda'>Koliko sebe smatrate humanim?</legend>
        <label class="container1">1
            <input type="radio" checked="checked" name="radio" value="1">
            <span class="checkmark"></span>
        </label>
        <label class="container1">2
            <input type="radio" name="radio" value="2">
            <span class="checkmark"></span>
        </label>
        <label class="container1">3
            <input type="radio" name="radio" value="3">
            <span class="checkmark"></span>
        </label>
        <label class="container1">4
            <input type="radio" name="radio" value="4">
            <span class="checkmark"></span>
        </label>
        <label class="container1">5
            <input type="radio" name="radio" value="5">
            <span class="checkmark"></span>
        </label>
        <button id="submitanketa" >prijavi</button>
        <input type="hidden" name="id" value="form1" />
        <input type="hidden" name="MM_insert" value="form1" />
        <input type="hidden" value="{{csrf_token()}}" id="token">
    </div>
</fieldset>

Route associatted

Route::post('/post/post/ajax', 'AnketaController@anketa');

Here is the controller

class AnketaController extends Controller{
    public function anketa(Request $request)
{
    $glasanje = $request->get('unos');
    $id_kor= session()->get('user')->id;
    $proba = new Anketa();
    $proba->glasanje = $glasanje;
    $proba->id = $id_kor;
    $rez = $proba->ubacianketu();
}}

Model associated

class Anketa
{

public $id;
public $glasanje;
public $id_glasanje;
public $id_korisnik;
public $datum_glasanja;
public $odgovor;

public function ubacianketu()
{
    $rez = DB::table('anketa')
        ->insert ([
            'ocena' => $this->glasanje,
            'id_kor' => $this->id
        ]);
        return $rez;
}


}

JQuery is loaded in the template.

2
  • 1
    You're calling url: baseUrl + "/post/post/ajax", but you don't have baseUrl defined. Commented May 12, 2018 at 0:52
  • When I define it outside of $.ajax it puts 404 error. Commented May 12, 2018 at 17:27

1 Answer 1

1

its give error because before baseUrl variable you don't define baseUrl variable so first enter code heredefine baseUrl variable in your ajax script like this

var baseUrl = "{{URL::to('/')}}";

Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to add those Laravel marks inside the jscript file. Bear in my my jscript is external.
No its not work in external js file. So I think you will need to add static baseUrl path in your external js file.
You can add like this Var baseUrl = "localhost/project-name/public/"; And also define server URL because when you upload your code on server localhost path make comment in. Var baseUrl = "your server path";

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.