Ajax为什么会返回null?

我的Ajax响应有问题。我试图通过MySQL数据库中指定的ID号显示一个属性地址,但问题是,当我将id的值设置为静态的(例如,prop_id=2)时,就可以成功地从db检索出现的地址。但是,当我想通过文本框中输入的PROPORT_ID进行搜索时,我会一直收到NULL值。

html

       <td><label for="propertyId">Property Id:</label></td>
       <td><input name="p_propertyId" id="propertyId" onClick="suggestion_property_id()" onblur="get_property_address_byid()" size="40"></td> <!--onblur="get_idproperty()"-->
       <td><label for="prop_address">Property address:</label></td>
       <td><input name="p_address" id="prop_address" value="" size="40" readonly/></td>

ajax

    var $propertyId = $('#propertyId').val();
       //alert($propertyId);
       $.ajax({
                 type: 'POST',
                 url: 'propertyId.php',
                 data: {
                      type:'prop_address',
                      $propertyId:$propertyId,
                      },
                      async: true,
                      dataType: 'text',
                      success: function(res1){
                            var results = eval(res1);
                            document.getElementById('prop_address').value = results[0];
                            //get_idbooking();
                            alert($propertyId);
                            console.log(res1);
                     },
               }); 
            }

php

if($type == 'prop_address'){
       $prop_address = "no address";
       $propertyId =$_POST['propertyId'];
       $propertyId=155;          
       $properties = get_address_by_propertyid($propertyId);

       for($i=0; $i<sizeof($properties); $i++){
              $address_prop = str_replace(' ', ' ', $properties[$i]['prop_address']);
              if(strpos($propertyId, $address_prop) !== false){
                     $prop_address = $properties[$i]['propertyId'];
                     break;
              }
       }
       $res1 = array($address_prop, $propertyId);
       echo json_encode($res1);
       //echo json_encode($address);
}

Be sure to heck the incoming data on the server side with, for example, print_r($_POST). It looks like you are sending:

data: {
    type:'prop_address',
    $propertyId:$propertyId,
}

but checking for

$_POST['propertyId']

I don't think they match.

The data that you are sending in the Ajax request seems to be wrong -

$propertyId:$propertyId,
^^^^^^^^^^^

should be -

propertyId:$propertyId,

In your ajax block, you're passing across the $propertyId value as both name and value. So, if it's 155, for example, your data block is effectively being sent to the server as:

data: {
    type: 'prop_address',
    155: 155
}

If you update the data block to name the propertyId parameter correctly, then your php script should pick it up.

data: {
    type: 'prop_address',
    propertyId: $propertyId
}

If you run in to these sort of issues in future (script works ok with value hard-coded but not when it's passed from elsewhere) a good place to start debugging is to have the php script print_r($_POST) or var_dump($_POST) to ensure you're passing across the parameters you think you are.

You assign $propertyId to wrong key:

Should be:

...
data: {
    type:'prop_address',
    propertyId: $propertyId
    // $propertyId: $propertyId
 },
...