简体   繁体   中英

Calling Php from jQuery (ajax)

Hi all you eminent StackOverflowers!

I am wondering if it is possible to call a php-script running a asp.net application using jQuery (Win7,VS2010,IIS)? Have tried some various tutorials and one setup is the one below:

My jQuery ajax-call:

$.ajax(
{
    post: "GET",
    url: "js/script.php"
}).done(function (data) {
    alert(data);
}).fail(function (jqXHR, textStatus, errorThrown) {
    alert(textStatus);
});

My Php-script:

<?php echo '<div id="test">Hello, World!</div>';?>

Calling the ajax-function just alerts the raw php-script text, ie

"<?php echo '<div id="test">Hello, World!</div>';?>"

And doesn't effect anything else.

What am I doing wrong? I have understood that it SHOULD be possible to run this on an asp-server.

(PS. I have looked into the suggested topics when posting this but noone have written that they get the actual php-text back DS.)

You cannot run PHP script on an "ASP-Server" that doesn't have PHP installed. Install PHP and everything will work.

Manual might be found on php.net .

You have to specify data-type into your ajax call.
If you don't see anything see into your console some error that retrieve from your call
Try this:

$.ajax(
{
    post: "GET",
    url: "js/script.php",
    dataType : 'html'
}).done(function (data) {
    alert(data);
}).fail(function (jqXHR, textStatus, errorThrown) {
    alert(textStatus);
});

when you get raw php while executing php script .. it simply means that for some reasons you php is not being getting interpreted .

Check if php server is installed and configured properly

Here is some stuff that can help you

 function showState(str){
if(str.length==0){
    document.getElementById("country").innerHTML="";
    return;
}
if (typeof XMLHttpRequest != "undefined"){
  xmlHttp= new XMLHttpRequest();
  }
else if (window.ActiveXObject){
  xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
   }
  if (xmlHttp==null){
  alert("Browser does not support XMLHTTP Request");
  return;
      } 
      var url="request.php";
 url +="?count=" +str;
   xmlHttp.onreadystatechange = stateChange;
   xmlHttp.open("GET", url, true);
     xmlHttp.send(null);
    }
  function stateChange(){   
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
    document.getElementById("state").innerHTML=xmlHttp.responseText;   
     }   
      }
    <select name="country" id="country" onchange="showState(this.value)">
    <option value="0">ChooseProject</option>
     <c:forEach items="${al }" var="v">
        <option value="${v.projectid}">${v.projectname}</option>
         </c:forEach>
    </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM