JQuery ajax request to php won't execute?












2















I need to load items from MySQL to a select element. so i wrote an ajax code and a php code. the problem is anywhere of the php code doesn't execute. no errors also. (the url is correct).
please help me.
Here is the jQuery code



$('#loadButton').click(function () {
console.log("clicked"); //this prints
$.ajax({
url: "api/service/RoomTypeService.php",
method: "POST",
async: true,
data: "&operation=getAll",
dataType: "json"
}).done(function (resp) {
alert(resp);
console.log(resp);
for (let i = 0; i < resp.length; i++) {
alert(resp); //this won't run
let roomType = resp[i];
$('#roomTypeCombo').append('<option>' + roomType[1] + '</option>');
}
});
});


and this is complete php code



<?php

require_once __DIR__."/../dto/RoomTypeDTO.php";
require_once __DIR__."/../business/impl/RoomTypeBOimpl.php";


echo "Here in service"; //doesn't execute

$roomTypeBo=new RoomTypeBOimpl();

$method=$_SERVER['REQUEST_METHOD'];

switch ($method){
case "POST":
$operation=$_POST["operation"];
switch ($operation){
case "getAll":
echo json_encode($roomTypeBo->getAllRoomTypes());
break;
}
break;
case "GET":

}









share|improve this question























  • change this data: "&operation=getAll", to data: {operation: "getAll"}, (which won't solve not reaching "Here in service", though. I know)

    – Jeff
    Nov 24 '18 at 14:11













  • When you call the php script directly in browser, do you then see "Here in service"? And what do you see now in network tab as return after executing the ajax?

    – Jeff
    Nov 24 '18 at 14:13













  • Why do you think the php part isn't executed? Did you check the network tab of the developer tools?

    – Andreas
    Nov 24 '18 at 14:31











  • @Jeff. Thanks. The result prints in network tab. But it doesn't come to done method. So i added a fail method in ajax and now it runs instead of done.

    – Tharindu Eranga
    Nov 24 '18 at 14:41











  • The fail handler (one of its arguments) will tell you what the problem is

    – Andreas
    Nov 24 '18 at 14:42
















2















I need to load items from MySQL to a select element. so i wrote an ajax code and a php code. the problem is anywhere of the php code doesn't execute. no errors also. (the url is correct).
please help me.
Here is the jQuery code



$('#loadButton').click(function () {
console.log("clicked"); //this prints
$.ajax({
url: "api/service/RoomTypeService.php",
method: "POST",
async: true,
data: "&operation=getAll",
dataType: "json"
}).done(function (resp) {
alert(resp);
console.log(resp);
for (let i = 0; i < resp.length; i++) {
alert(resp); //this won't run
let roomType = resp[i];
$('#roomTypeCombo').append('<option>' + roomType[1] + '</option>');
}
});
});


and this is complete php code



<?php

require_once __DIR__."/../dto/RoomTypeDTO.php";
require_once __DIR__."/../business/impl/RoomTypeBOimpl.php";


echo "Here in service"; //doesn't execute

$roomTypeBo=new RoomTypeBOimpl();

$method=$_SERVER['REQUEST_METHOD'];

switch ($method){
case "POST":
$operation=$_POST["operation"];
switch ($operation){
case "getAll":
echo json_encode($roomTypeBo->getAllRoomTypes());
break;
}
break;
case "GET":

}









share|improve this question























  • change this data: "&operation=getAll", to data: {operation: "getAll"}, (which won't solve not reaching "Here in service", though. I know)

    – Jeff
    Nov 24 '18 at 14:11













  • When you call the php script directly in browser, do you then see "Here in service"? And what do you see now in network tab as return after executing the ajax?

    – Jeff
    Nov 24 '18 at 14:13













  • Why do you think the php part isn't executed? Did you check the network tab of the developer tools?

    – Andreas
    Nov 24 '18 at 14:31











  • @Jeff. Thanks. The result prints in network tab. But it doesn't come to done method. So i added a fail method in ajax and now it runs instead of done.

    – Tharindu Eranga
    Nov 24 '18 at 14:41











  • The fail handler (one of its arguments) will tell you what the problem is

    – Andreas
    Nov 24 '18 at 14:42














2












2








2








I need to load items from MySQL to a select element. so i wrote an ajax code and a php code. the problem is anywhere of the php code doesn't execute. no errors also. (the url is correct).
please help me.
Here is the jQuery code



$('#loadButton').click(function () {
console.log("clicked"); //this prints
$.ajax({
url: "api/service/RoomTypeService.php",
method: "POST",
async: true,
data: "&operation=getAll",
dataType: "json"
}).done(function (resp) {
alert(resp);
console.log(resp);
for (let i = 0; i < resp.length; i++) {
alert(resp); //this won't run
let roomType = resp[i];
$('#roomTypeCombo').append('<option>' + roomType[1] + '</option>');
}
});
});


and this is complete php code



<?php

require_once __DIR__."/../dto/RoomTypeDTO.php";
require_once __DIR__."/../business/impl/RoomTypeBOimpl.php";


echo "Here in service"; //doesn't execute

$roomTypeBo=new RoomTypeBOimpl();

$method=$_SERVER['REQUEST_METHOD'];

switch ($method){
case "POST":
$operation=$_POST["operation"];
switch ($operation){
case "getAll":
echo json_encode($roomTypeBo->getAllRoomTypes());
break;
}
break;
case "GET":

}









share|improve this question














I need to load items from MySQL to a select element. so i wrote an ajax code and a php code. the problem is anywhere of the php code doesn't execute. no errors also. (the url is correct).
please help me.
Here is the jQuery code



$('#loadButton').click(function () {
console.log("clicked"); //this prints
$.ajax({
url: "api/service/RoomTypeService.php",
method: "POST",
async: true,
data: "&operation=getAll",
dataType: "json"
}).done(function (resp) {
alert(resp);
console.log(resp);
for (let i = 0; i < resp.length; i++) {
alert(resp); //this won't run
let roomType = resp[i];
$('#roomTypeCombo').append('<option>' + roomType[1] + '</option>');
}
});
});


and this is complete php code



<?php

require_once __DIR__."/../dto/RoomTypeDTO.php";
require_once __DIR__."/../business/impl/RoomTypeBOimpl.php";


echo "Here in service"; //doesn't execute

$roomTypeBo=new RoomTypeBOimpl();

$method=$_SERVER['REQUEST_METHOD'];

switch ($method){
case "POST":
$operation=$_POST["operation"];
switch ($operation){
case "getAll":
echo json_encode($roomTypeBo->getAllRoomTypes());
break;
}
break;
case "GET":

}






php jquery ajax






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 14:09









Tharindu ErangaTharindu Eranga

266




266













  • change this data: "&operation=getAll", to data: {operation: "getAll"}, (which won't solve not reaching "Here in service", though. I know)

    – Jeff
    Nov 24 '18 at 14:11













  • When you call the php script directly in browser, do you then see "Here in service"? And what do you see now in network tab as return after executing the ajax?

    – Jeff
    Nov 24 '18 at 14:13













  • Why do you think the php part isn't executed? Did you check the network tab of the developer tools?

    – Andreas
    Nov 24 '18 at 14:31











  • @Jeff. Thanks. The result prints in network tab. But it doesn't come to done method. So i added a fail method in ajax and now it runs instead of done.

    – Tharindu Eranga
    Nov 24 '18 at 14:41











  • The fail handler (one of its arguments) will tell you what the problem is

    – Andreas
    Nov 24 '18 at 14:42



















  • change this data: "&operation=getAll", to data: {operation: "getAll"}, (which won't solve not reaching "Here in service", though. I know)

    – Jeff
    Nov 24 '18 at 14:11













  • When you call the php script directly in browser, do you then see "Here in service"? And what do you see now in network tab as return after executing the ajax?

    – Jeff
    Nov 24 '18 at 14:13













  • Why do you think the php part isn't executed? Did you check the network tab of the developer tools?

    – Andreas
    Nov 24 '18 at 14:31











  • @Jeff. Thanks. The result prints in network tab. But it doesn't come to done method. So i added a fail method in ajax and now it runs instead of done.

    – Tharindu Eranga
    Nov 24 '18 at 14:41











  • The fail handler (one of its arguments) will tell you what the problem is

    – Andreas
    Nov 24 '18 at 14:42

















change this data: "&operation=getAll", to data: {operation: "getAll"}, (which won't solve not reaching "Here in service", though. I know)

– Jeff
Nov 24 '18 at 14:11







change this data: "&operation=getAll", to data: {operation: "getAll"}, (which won't solve not reaching "Here in service", though. I know)

– Jeff
Nov 24 '18 at 14:11















When you call the php script directly in browser, do you then see "Here in service"? And what do you see now in network tab as return after executing the ajax?

– Jeff
Nov 24 '18 at 14:13







When you call the php script directly in browser, do you then see "Here in service"? And what do you see now in network tab as return after executing the ajax?

– Jeff
Nov 24 '18 at 14:13















Why do you think the php part isn't executed? Did you check the network tab of the developer tools?

– Andreas
Nov 24 '18 at 14:31





Why do you think the php part isn't executed? Did you check the network tab of the developer tools?

– Andreas
Nov 24 '18 at 14:31













@Jeff. Thanks. The result prints in network tab. But it doesn't come to done method. So i added a fail method in ajax and now it runs instead of done.

– Tharindu Eranga
Nov 24 '18 at 14:41





@Jeff. Thanks. The result prints in network tab. But it doesn't come to done method. So i added a fail method in ajax and now it runs instead of done.

– Tharindu Eranga
Nov 24 '18 at 14:41













The fail handler (one of its arguments) will tell you what the problem is

– Andreas
Nov 24 '18 at 14:42





The fail handler (one of its arguments) will tell you what the problem is

– Andreas
Nov 24 '18 at 14:42












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458993%2fjquery-ajax-request-to-php-wont-execute%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458993%2fjquery-ajax-request-to-php-wont-execute%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno