2016-11-07 11 views
1

からPHPを呼び出す: (イムは「試験」のアラートを取得し、それはそれだ:()2.php上私は非常に基本的ないろいろ書いをしようとしているし、それが動作しませんAJAX

<script name="text/javascript"> 
    function myFunction(part_id, product_id, type) 
    { 
     alert('test'); 
     $.ajax({ 
      type: 'post', 
      url: '2.php', 
      data: {lname: "www", name: "Natalie"}, 
      complete: function (txt) { 
       alert("complete"); 
       alert(txt); 
      } 
     }); 
    } 
</script> 

私が唯一持っています(同じディレクトリに)1行:。

echo "test has been run"; 
+1

あなたは2.php –

+1

待ちに使用するつもりリクエストメソッドのタイプを初期化する必要があり、http://stackoverflow.com/questions/66420/how-do-([コンソールをチェック] javascript-debugger-in-google-chrome)にエラーがないかどうかを確認します。 –

+0

Firebugアドオンをfirefoxブラウザに追加し、コンソールを確認してください。 – Akshay

答えて

0

success event.Itで使用するPHPの結果を取得します

See the different between success() & complete()

<script name="text/javascript"> 
    function myFunction(part_id, product_id, type) 
    { 
     alert('test'); 
     $.ajax({ 
      type: 'post', 
      url: '2.php', 
      data: {lname: "www", name: "Natalie"}, 
      complete: function (txt) { 
       //do something 
      }, 
      success: function (result) { 
       alert("success"); 
       alert(result);//test has been run 
      } 
     }); 
    } 
</script> 
0

アヤックスは、オブジェクトのデータを返します。

Object {readyState: 4, responseText: "test has been run", status: 200, statusText: "OK"} 

あなたは、そのオブジェクトからの応答テキストを取得する必要があります。

function myFunction(part_id, product_id, type) 
{ 
    alert('test'); 
    $.ajax({ 
     type: 'post', 
     url: '2.php', 
     dataType:'text', 
     data: {lname: "www", name: "Natalie"}, 
     complete: function (txt) { 
     console.log(txt); 
      alert("complete"); 
      alert(txt.responseText); 
     } 
    }); 
} 
+0

ありがとう、私はそれを試みたが、それは私のためには動作しません。私はおそらくそこに何か基本的なものを見逃している。 –

関連する問題