1

I have this ajax request

var token = '{{ Session::token() }}';
var urlAdd = '{{ route('postSocialWorker') }}';

$("#save").click(function () {
    $.ajax({
        type: 'post',
        url:  urlAdd,
        data:{
            'name': $('input[name=name]').val(),
            'email': $('input[email=email]').val(),
            'store_id': $("select['name=store_id']").val(),
            _token: token
        },
        success: function (data) {
            if ((data.errors)) {
                $('.error').removeClass('hidden');
                $('.error').text(data.errors.name);
            }
            else {
                $('.error').addClass('hidden');
                $('#table').append("<tr class='item" + data.id + "'><td>" + data.id + "</td><td>" + data.name + "</td><td><button class='edit-modal btn btn-info' data-id='" + data.id + "' data-name='" + data.name + "'><span class='glyphicon glyphicon-edit'></span> Edit</button> <button class='delete-modal btn btn-danger' data-id='" + data.id + "' data-name='" + data.name + "'><span class='glyphicon glyphicon-trash'></span> Delete</button></td></tr>");
            }
        },
    });

and this is my controller

public function addSocialWorker(Request $request)
{
   $rules = array(
    'name' => 'required|alpha_num',
    'email' => 'required|email|unique:users',
    'store_id' => 'required|numeric'
    );

   $validator = Validator::make(Input::all(), $rules);
   if ($validator->fails())
    return Response::json(array(
        'errors' => $validator->getMessageBag()->toArray()
        ));
else {
    $data = new User();
    $data->name = $request->name;
    $data->email = $request->email;
    $data->status = 1;
    $data->role_id = 2;
    $data->store_id= $request->store_id;
    $data->save();
    return response()->json($data);
}

}

but i get this error

XMLHttpRequest cannot load javascript:void(0). Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

Any ideas? i'm using laravel

TheBAST
  • 2,680
  • 10
  • 40
  • 68
  • I think you meant to have `url: urlAdd`, not `url: 'urlAdd'`. Also, jQuery definitely does not use a `_token` AJAX config property so not sure what that's doing there – Phil Feb 08 '17 at 04:12
  • Still getting an error – TheBAST Feb 08 '17 at 04:14
  • You can prefer this [link] (http://stackoverflow.com/questions/20041656/xmlhttprequest-cannot-load-file-cross-origin-requests-are-only-supported-for-ht) – Pramod Patil Feb 08 '17 at 04:16
  • Please update the code in your question. @PramodPatil I highly doubt it's a CORS error. OP appears to be attempting to use a local Laravel route URL – Phil Feb 08 '17 at 04:17
  • 1
    @A.Sand Could you please update your code as per above suggestion given – Pramod Patil Feb 08 '17 at 04:23
  • What do you want me to provide? – TheBAST Feb 08 '17 at 04:25
  • If you tried the suggestion from the first comment, update your question to reflect the current state of your code. If the error message has changed, please provide the new one – Phil Feb 08 '17 at 04:33
  • So what should i put in the token? field? – TheBAST Feb 08 '17 at 04:38
  • Should i remove it? – TheBAST Feb 08 '17 at 04:39
  • @A.Sand sorry, your initial formatting made it look like you were passing a `_token` config property. I've fixed the formatting in your question to make it clearer. You should definitely be using `url: urlAdd` instead of `url: 'urlAdd'` though – Phil Feb 08 '17 at 04:45
  • Already changed it sir – TheBAST Feb 08 '17 at 04:56
  • Can you use your browser's *"view-source"* option to see what value is assigned to your `urlAdd` variable? – Phil Feb 08 '17 at 04:59
  • Ah, just thought of something. Try `$('#save').click(function(e) { e.preventDefault(); // and the rest })`. I have a feeling you have `href="javascript:void(0)"` somewhere and that's being used in the request – Phil Feb 08 '17 at 05:04
  • Thank you i just removed the javascrip:void(0) because I forgot that it's using jquery thanks – TheBAST Feb 08 '17 at 06:55

0 Answers0