I'm trying my first PHP MVC project from scratch. Everything is working fine... everything but ajax calls. When I try to POST I get a 404 error, but ONLY using AJAX; I've made the same calls using html form and it works.
This is my request:
$.ajax({
type: "get",
url: "/Prueba/Bodega/createPost",
data: data,
success: function (data) {
and this is on my view (works if I comment the "preventDefault" line on js)
<form action="<?=BASE_URL?>Bodega/createPost" method="POST">
Both methods calls the controller Bodega, function createPost ("http://localhost/prueba/Bodega/createPost").
I think this is because of the .htaccess file using friendly urls but I coundn't get the solution.
This caused because the we server treats the request URLs in a case sensitive way. That said:
/Prueba/Bodega/createPost and /prueba/Bodega/createPost are two different URLs.
Please change /Prueba/Bodega/createPost to /prueba/Bodega/createPost and it should get fixed.
Please use POST on ajax request.
Related
So, I'm trying to send a post request to a php script of mine.
On the web I'm in a page like: https://yyy.yyy/test-page
My ajax url is set as such:
url: "https://yyy.yyy/test.php"
But then it returns a 404 error with the following url:
https://yyy.yyy/test-page/test.php
And it definitely won't find in the php file in that path, since it is located at root. Moving the php to another folder is not an option.
This website is located in a WordPress server.
Here's the js code:
$.ajax({
url: "https://yyy.yyy/test.php",
method: "POST",
type: "Json",
data: data,
success: function (res) {...}
});
If you're wanting to make use of Ajax and are running WordPress, you should look into writing a plugin and making use of the hooks and functions available to make this easier as WordPress will do a lot of heavy lifting.
I see you mentioned it can't be moved, but if it's at all possible to at least try and replicate the PHP code into a plugin (or a theme although that's less ideal and might not work properly) then it'll make things easier.
Take a look at using Ajax in plugins.
The JavaScript (jQuery) bit:
$.ajax({
data: {
action: 'my_ajax_request'
},
type: 'post',
url: SOWP_AJAX.admin_ajax,
success: function(response) {
console.warn( response );
}
});
The PHP bit (Part 1):
This ensures that the URL that you post your Ajax function to is mapped to an actual URL. In this case, the WordPress Ajax handler at admin-ajax.php.
wp_localize_script(
'sowp_js', // this is the script name that should be used to enqueue the JavaScript code above
'SOWP_AJAX',
array(
'admin_ajax' => admin_url('admin-ajax.php')
)
);
The PHP bit (Part 2):
Put this inside a plugin or theme file that is activated. The hook my_ajax_request must match the request action in the Ajax request and the function name so it's called correctly.
add_action( 'wp_ajax_my_ajax_request', 'my_ajax_request' ); // the hook that is triggered when the request is made
function my_ajax_request() {
echo 'my ajax request works'; // print something out
wp_die(); // stop script execution
}
Once you've got all of the above in place, when the Ajax request is working and runs, you should see my ajax request works printed in your browsers console. If it returns 0 then it means the request failed somewhere. If it does fail, it usually means the action hook has been registered and called so something might be spelt wrong or missing.
I have 2 projects, let's call them backend and frontend. Backend is a Symfony3 project and Frontend is a simple html page with javascript in it.
I would like to call a peculiar url of the backend from the frontend so that it would return a string (a url).
So what I tried to do is a simple ajax get request with the backend url I would like to request but this returns
SyntaxError: Unexpected token "<"
(for html dataType) and
SyntaxError: Unexpected token.
(for json and jsonp dataType).
I tried with dataType json, jsonp, html adapting each time the controller response to answer respectively json, json, and html.
When I try this request from postman everything works just fine and i get either my json or html.
There is obviously something I don't understand.
Is there anybody that could explain me what I do wrong and how I could do it.
Below is my frontend project ajax request (for html in that case but I've let the commented json part I've jused for test in case of...)
$.ajax({
url: 'http://sevignemiroir.local/display',
type: 'GET',
dataType: "html",
//dataType: "jsonp"
success: function(response) {
console.log('success');
console.log(response);
},
error: function (response) {
console.log('error');
console.log(response);
}
})
And this my response from my backend symfony controller
public function indexAction()
{
//return $this->json('toto');
return $this->render('display/test.html.twig', [
'toto' => 'toto'
]);
}
the test.html.twig file :
<div>test</div>
I am a bit confused :
Do I have to apply a specific format to the response to make it work?
Is there something I should configure in my controller to allow request from outside the project?
Does the problem come from a Cross-Domain issue?
I've heard about FOSRestBundle, is it mandatory to use such bundle to enable data send from outside the project?
What are the best practices for calling data from a project to another project?
These are the questions I'm getting confused with and if anyone would be kind enough to explain it, that would be greatly appreciated!
I don't know if that's important but I'm using MAMP and both project have virtualhosts configured
Many thanks to those that will take time to read this :)
It now works but... I don't know why. I was installing various bundle on symfony (FOSRest, NelmioCors, JMS) and tried a jquery cross-plugin plugins, nohing was working but when I removed al of them, it now worked.. :/
I am developing a website for a client using html, js and php. For any database call i am using ajax call
$.ajax({
type: 'POST',
url: "index2.php",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, status) {
dosomething(msg);
},
error: function (msg, status) {
console.log("failure");
console.log(msg);
}
});
and php pages returns me the output. The problem is that index.html page url is not getting changed.....I want it to change say index.html?id=23 (creating query string in html page)
Basically my aim to make it SEO friendly url like index.html/helloppl
What is the best method to do this. While googling i found link for doing....still i need to create query string here...any help is appreciated.
Creating SEO fridenly url.
Creating query string in html
The problem is that index.html page url is not getting changed. I want it to change say index.html?id=23 (creating query string in html page)
Ans: if you want to change the url like you have mention above this the ajax will not the way to do this . This can be done in simple way go to the url i have provided and look at the topic.
http://html.net/tutorials/php/lesson10.php
Basically my aim to make it SEO friendly url like index.html/helloppl. What is the best method to do this. While googling i found link for doing this. I still need to create query string here.
Ans: This can be done with htaccess. Go To The Link
Jquery ajax() messing with my .htaccess mod_rewrite
PJAX's documentation states that Github uses $.pjax.submit() in submitting a Gist form. A desired feature of an ajax form submission which Github nicely implements is that the URL redirects from the form's action to a newly created URL (in this case, one containing the newly server-side created gist ID).
For example, from this:
https://gist.github.com/gists // form action
to this:
https://gist.github.com/tim-peterson/5019589 //assume this ID is generated server side
I've gotten this to work similarly on my site (i.e., the page itself redirects to the equivalent of https://gist.github.com/tim-peterson/5019589) but I can't redirect the URL (it stays like https://gist.github.com/gists).
Is this entirely a server-side issue (setting headers?) or is there something in pjax that I'm missing? I'm using a version of pjax I downloaded today so it can't be that i'm using a buggier version of pjax.
Did you find any solution to this?
I had the similar issue.
Basically you need to set X-PJAX-URL property in response header.
It's value should be same as Request URL.
In Laravel, I did it like this...
App::after(function($request, $response)
{
$response->headers->set('X-PJAX-URL', $request->getRequestUri());
});
There may be a more-elegant / proper way to do this with $.pjax.submit(), but this is the solution I've been using without any problems. I serialize the form inputs prior to submission, then POST them to my server. Then check the reply from the server to make sure I don't need to prevent the user from going forward before calling my PJAX to continue.
$("#save").click(function(e) {
e.preventDefault();
dataString = $("#form").serialize();
$.ajax({
type: "POST",
url: $("#form").attr("action"),
data: dataString,
dataType: "json",
success: function(data)
{
// Handle the server response (display errors if necessary)
$.pjax({
timeout: 2000,
url: data.url,
container: '#main-content'
});
}
});
});
It happens if response HTML doesn't contain initial PJAX container
I would like to using (Ajax) PHP or Javascript, Post information to http://en.lernu.net/cgi-bin/vortaro.pl then read the results back (Not from lernu.net).
I am trying to learn Ajax, PHP + Javascript, Nobody there know's how to help me. I would very much like doing this without touching Lernu's code, So if there is a way to do it all on my code, that would be great!
You need to proxy the request due to browsers preventing cross-domain ajax calls.
You can either do this with a PHP page on your site or configure url rewrite rules for your webserver.
You maybe able to do a simple post to your url with jquery in following ways:
$.ajax({
type: "POST",
url: "http://en.lernu.net/cgi-bin/vortaro.pl",
data: "name=John&age=21",
success: function(msg){
alert( "Data Posted to server: " + msg );
// you may additionally call other javascript methods here to do modifications to your page based on your request
}
});
Jquery is an excellent framework for javascript and I would highly recommend using it for most of your functionality. You might want to readup a bit about javascript and then start up with jquery.
You need to write a PHP script in your domain that forwards your POST to http://en.lernu.net/cgi-bin/vortaro.pl, then forwards their response back to the client.
You can then send an AJAX POST to your server with jQuery.