Ajax URL not correct with subfolder implemented - ajax

I implemented my project in a subfolder. I am putting it in /var/www/html/subfolder
The link to my subfolder project looks like this
https://mytestingsite.com/subfolder
Ajax
$.ajax({
url: "/subfolder/testroute",
data: data,
dataType: json
});
When i call an ajax, my url will be
url : "/subfolder/testroute"
But the url return https://mytestingsite.com/testroute
I exptected the url return as https://mytestingsite.com/subfolder/testroute
I found that if i put my url as
url : "/subfolder/subfolder/testroute"
Then the url only will return https://mytestingsite.com/subfolder/testroute
Is anything wrong with my route?

Try this:
var baseurl = window.location.protocol + "//" + window.location.host;
$.ajax({
url: baseurl + "/subfolder/testroute",
data: data,
dataType: json
});

Related

Cant get URL parameters in CodeIgniter

I tried to access an id from a URL "myfolder/mycontroller/mymethode/123" which I call into an AJAXcall.
I cannot access them, in "mycontroller" under the "mymethode". I tried to output $_GET, $_POST, $this->input->get(), $this->input->post(), but all array are empty.
In the Controller/mycontroller I have this
public function mymethode($listid=false)
{
echo "listid: $listid";
print_r($_GET);
print_r($_POST);
print_r($this->input->get());
print_r($this->input->post());
}
The Ajax call is this and is ok with Status 200.
$.ajax({
url: http://mydomein.com/myfolder/mycontroller/mymethode/123,
type: "POST",
method: 'post',
data: form + "&" + additional_data + csrfName + "=" + csrfHash,
dataType: 'json',
cache: false,
success: function(res){...
If I tried to open the URL directly, I have the same problem.
What can the reason for it be?
Use this (if this is not HMVC)
in route.php
# You may need first route since you're accepting null on method
$route['mymethode] = 'mycontroller/mymethode';
$route['mymethode/(:num)'] = 'mycontroller/mymethode/$1';
In AJAX
url: http://mydomein.com/mymethode/123,
Make sure your sites run without index.php
In controller
public function mymethode($listid=null)

Ajax in Django : url not found

I am coding a small Django project where an user can select an object and save it in a database. I am trying to implement an Ajax call on a button to delete this object if necessary.
I am doing it step by step, debugging with the console.
my urls:
app_name = 'register'
urlpatterns = [
path('<int:user_id>/', views.account, name='account'),
path('/delete/', views.delete, name='delete'),
]
my view.py:
def delete(request):
data = {'success': False}
if request.method=='POST':
product = request.POST.get('product')
print(product)
data['success'] = True
return JsonResponse(data)
my ajax.js:
$("#form_id").on('submit', function(event) {
event.preventDefault();
var product = 'coca-cola'
console.log('ok till this point')
$.ajax({
url: '{% url "register/delete" %}',
type: "POST",
data:{
'product':product,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
datatype:'json',
success: function(data) {
if (data['success'])
console.log('working fine')
}
});
});
My view isn't doing much for now but I haven't any knowledge about Ajax and I am doing it one step at a time.
This is the error I get in the console:
jquery.min.js:2 POST http://127.0.0.1:8000/register/6/%7B%%20url%20%22register/delete%22%20%%7D 404 (Not Found)
As far as I understand, Ajax can't find my url: '{% url "register/delete" %}'.
I have tried '{% url "register:delete" %}' with no luck either.
I found an answer after some tweaking, I defined my url before the Ajax call and then pass it in it:
$("#form_id").on('submit', function(event) {
event.preventDefault();
var product = 'coca-cola'
var url = '/register/delete/'
console.log( url)
$.ajax({
url: url,
type: "POST",
data:{
'product':product,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
datatype:'json',
success: function(data) {
if (data['success'])
console.log('working fine')
}
});
});
Also you can add just the string of url to "url" parameter without characters {% url %}. Maybe you copied the code from pattern Django and added it to JS-file. So it does not work.

Django - Ajax: sending url params in data

I am trying to send a "GET" and "POST" requests to Django server via Ajax.
First, I provide the URLConf:
url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'),
Now, the AJAX part. Previously, I used to do it this way (avoiding sending params in data)
$.ajax({
type: "GET",
url: '/write_to_file/' + file_name + '/' + content ,
data: {},
success: function(data){
alert ('OK');
},
error: function(){
alert("Could not write to server file " + file_name)
}
});
Up to a certain moment, I was satisfied by this method, but now it is important for me to pass on file_name and content via the "data" variable and for some reason I get 404 Error.
$.ajax({
type: "GET",
url: '/write_to_file/',
data: {'file_name':file_name, 'content':content},
success: function(data){
alert ('OK');
},
error: function(){
alert("Could not write to server file " + file_name)
}
});
Error on server-side:
Not Found: /write_to_file/
[15/Apr/2016 14:03:21] "GET /write_to_file/?file_name=my_file_name&content=my_content HTTP/1.1" 404 6662
Error on client-side:
jquery-2.1.1.min.js:4 GET http://127.0.0.1:8000/write_to_file/?file_name=my_file_name&content=my_content 404 (Not Found)
Any ideas why? Is it something wrong with the ajax syntax or it has somehow to do with the URLConf ?
url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'),
is now wrong, the url you are sending the post request to is: /write_to_file/
url(r'^write_to_file/$',generalFunctions.write_to_file, name ='write_to_file'),
is what you'd want i think!

jquery ajax get XML from another domain

Hey all here is my code i have to read an XML file from a Vimeo website:
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://vimeo.com/api/v2/video/51229736.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('video').each(function(){
var thumbURL = $(this).attr('thumbnail_small');
alert(thumbURL);
$('#vidThumb').html('<img src="' + thumbURL + '">');
});
},
error: function(err) {alert('err');}
});
});
The XML looks like this:
<videos>
<script/>
<video>
<id>51229736</id>
<title>CHATT HISTORY CENTER FILMS CAVALIER</title>
<description/>
<url>http://vimeo.com/51229736</url>
<upload_date>2012-10-11 13:08:51</upload_date>
<thumbnail_small>http://b.vimeocdn.com/ts/353/072/353072229_100.jpg</thumbnail_small>
<thumbnail_medium>http://b.vimeocdn.com/ts/353/072/353072229_200.jpg</thumbnail_medium>
......
</video>
</videos>
Problem being is that it errors out. I'm sure its because of the different domain name trying to read it so how can i fix that in order to do that?
You can not do that through jQuery's ajax across different domains using XML , you can use callback=? to get jsonp response back like in the other answer , if it is possible to get json response from that url
You should have no problem getting an XML response from your server side , you should probably try that route
Achieved it by doing the following:
var vimeoVideoID = '51229736';
$.getJSON('http://www.vimeo.com/api/v2/video/' + vimeoVideoID + '.json?callback=?', {format: "json"}, function(json) {
$("#vidThumb").attr('src', json[0].thumbnail_small);
});
I think the answer is in setting the callback to "?" at least it usually is for me. This at least works with JSON. And if it were JSON, this is how I would do it:
var query = 'http://vimeo.com/api/v2/video/51229736.xml&callback=?';
$.ajax({
url: query,
type: 'GET',
dataType: 'json',
success: function(s) {
console.log('success' + s)
},
error: function(e) { console.log('something went wrong!', e)}
});

ajax POST method is not working

I am trying to send the data via ajax POST method my code is
$.ajax({
url: myUrl + "?token=" + accessToken + "&key=" +dev_key,
dataType: 'jsonp',
type: 'POST',
data: sendXML,
success: function () {
alert("z");
}
});
But the type: 'POST' is not working I am getting the following error on console:
Status Code:405 HTTP method GET is not supported by this URL
Have you tried using $.post ?
Example:
$.post(
myUrl,
{
token: accessToken,
key: dev_key
},
function(result){
alert(z)
}
)
P.S. Isn't ? missing after myUrl?
i think you forgot the ? in the token key like this
mySql + "?token="
otherwise, try this:
jQuery.post(
myUrl + "?token=" + accessToken + "&key=" +dev_key,
sendXML,
function() {
alert('z');
},
'JSONP'
);

Resources