2

I am new in laravel. I want to create an api using laravel using ajax call. But I request ajax call,url shows invalid server path. Here is my code
My Route file :

Route::get("a/b","AController@c");

My js file :

var base = "public/index.php";
var url = base + "a/b";
$.ajax({
  url : url,
  dataType: "json",
  timeout: 10000,
  error:function(){  alert("Error getting from server") }
}).done(function(resp){

});

Suppose I am in following urls:

domain.com/dev/lar/public/index.php/c/d

Then I call this ajax , then url will redirect to

domain.com/dev/lar/public/index.php/c/public/index.php/a/b

Here lar is my laravel app folder

** Note I am using NGINX server. My Server Admin do not rewrite this url. That's why I use public/index.php **
4
  • Try this code Route::get("/a/b","AController@c"); instead of Route::get("a/b","AController@c"); Commented Jan 22, 2017 at 4:18
  • This route will not working because my app folder is lots of sub folder after domain. ex- domain.com/dev/{laravel app folder} Commented Jan 22, 2017 at 4:23
  • why you use domain.com/dev/lar/public/index.php/c/d not domain.com/dev/lar/public/c/d. you make index.php a directory not a file??? Commented Jan 22, 2017 at 5:45
  • domain.com/dev/lar/public/c/d gives 404 error. after giving domain.com/dev/lar/public/index.php/c/d, routes works correctly Commented Jan 22, 2017 at 6:02

1 Answer 1

1

You are missing in controller declaration. You have to use backslash at first. For example

Route::get("/a/b","AController@c");

And in your ajax code url should be

var base = "domain.com/dev/lar/public/index.php";
var url = base + "/a/b";
Sign up to request clarification or add additional context in comments.

2 Comments

I think the problem was in your base url of ajax. I have updated the code. try this
You are right. I knew this method. but I do not want to fix my server base path in my code. it's a bad practice to fix base url path in code. If I change my domain, then I want to modify my code

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.