JS Error in path set in URL

On this link

I also know where the error is coming.

In this ajax we are setting this URL:

url: "/wp-admin/admin-ajax.php",

but that is taking us to the sub domain as main path, but the actual link is different.

html.example.com sub domain, for example.

But the WordPress is installed here:


so the URL that is being generated is:


It should have been:


Is there a way we can fix this?

Most likely, removing the leading slash should resolve it.

url: "wp-admin/admin-ajax.php",

The leading slash tells the browser to look relative to the root of the site, where if it’s missing, then it will look relative to the location the calling object is running from.

1 Like

Let me try this one. I will update you.

This one didn’t worked, but few days back I had a discussion here →

this one worked:


Yeah, that makes sense as javascript is closer to shell scripting, so the conventions are very similar…

1 Like

I have one more issue.

var a = "./wp-content/plugins/visa-plugin/assets/images/",

Now suppose the WordPress is installed here →


Aand a WordPress post will look like this:


Now it is throwing image URL here →


It should be throwing it here →


This additional WordPress post path is unwanted :


Is there a way to get out of it?

You’re going to have to edit the value. Something like (off the top of my head)

var a = "./".split('index').slice(0, -1).join('index') + "wp-content/plugins/visa-plugin/assets/images/";

That may not work, but essentially the work flow is:

  1. Get the working url (the “./”)
  2. Strip off everything after the index.php
  3. Add the images location
1 Like

I tried this, but still we are in the same position. same path generating.

I hope that there is a solution.

Well, I told you I wasn’t sure that code was going to work but the itemized process I gave you should work.

This code DOES essentially what I suggested. the first line is step 1, the second combines steps 2 and 3.

$var = "http://html.example.com/temp/2/index.php/2019/08/08";
$split = $var.split('index').slice(0, -1).join('index') + "wp-content/plugins/visa-plugin/assets/images/error_ico.png";

That code results in

Hi @codeispoetry, I’d suggest to use the built-in wp functions like plugins_url() then. Of course, you shouldn’t mix PHP and JS, but you might generate a simple JSON dictionary like e.g.

echo '<script>window.wpData = ' . json_encode([ 
  'pluginsUrl' => plugins_url() 
]) . ';</script>';

… which you can then access from anywhere in your actual JS.

1 Like

Actually it worked. Yesterday It looked not working, because shared hosting has some cache issues. In the morning I checked and it was working.

Can you please help me to understand what is happening at the coding level of this part →

"./".split('index').slice(0, -1).join('index') +

You could probably figure this out with some simple web searches (“javascript split”, “javascript slice”, “javascript join”) but essentially

  1. “./” gets the URL for the object being run (you should have figured that out by now)
  2. split takes that value and splits it into an array, split where “index” is
  3. slice pulls the corresponding elements from the array, 0 is equivalent to array[0]is the starting position, -1 tells it to extract all of the array elements EXCEPT the last one (hence everything after the index)
  4. join puts the array elements back into a string putting “index” as the separator - since you’ve only got one element, nothing got added to the string.

So essentially you have

  1. http://html.example.com/temp/2/index.php/2019/08/08/
  2. [“http://html.example.com/temp/2/”, “.php/2019/08/08/”"]
  3. [“http://html.example.com/temp/2/”]
  4. http://html.example.com/temp/2/

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.