2012-11-19 48 views
0

我想在Magento中對PHP webservice進行AJAX調用。這是我的PHP代碼片段。AJAX調用不工作(錯誤302)Magento

<?php 
$callbackUrl = "http://localhost/magento/webservices/NewCustomer1.php"; 
    //intiate oauth callback URL 
    $temporaryCredentialsRequestUrl = "http://localhost/magento/oauth/initiate?oauth_callback=". urlencode($callbackUrl); 
    $adminAuthorizationUrl = 'http://localhost/magento/admin/oauth_authorize'; 
    $accessTokenRequestUrl = 'http://localhost/magento/oauth/token'; 
    //Magento rest API URL 
    $apiUrl = 'http://localhost/magento/api/rest'; 
    //Consumer key and secret 
    $consumerKey = 's3xt7w8lwhfrrfzrfvwm3lrilkf66d5n'; 
    $consumerSecret = 'vr3eq1x899pz1cf4zzxjzx3q03t66r3n'; 
    //get customer attributes 
    $firstname=$_POST['fname']; 
    $lastname=$_POST['lname']; 
    $email=$_POST['email']; 
....... 

這裏是我的jQuery

$('#btnSubmit').click(function(){ 
    console.log("Submit Clicked"); 
    var fName=$('#firstname').val(); 
    var lName=$('#lastname').val(); 
    var email=$('#email_address').val(); 
    var password=$('#password').val(); 
    var pass_conf=$('#confirmation').val(); 
    var dataString = 'fname='+ fName + '&lname=' + lName + '&email=' + email + '&password=' +password+ '&webid=1&groupid=1'; 
    /*http://localhost/magento/webservices/Newcustomer1.php?fname=siri&lname=s&[email protected]&password=password123&webid=1&groupid=1*/ 
    //your validation code 
$.ajax({ 
     url: 'http://localhost/magento/webservices/Newcustomer1.php', 
     type: 'POST', 
     data: dataString, 
     success: function(data) { 
      $('#message').html(data); 
     } 
    }); 
}); 

在提交我得到302發現的錯誤。如果我在URL本身傳遞參數並將$ _REQUEST更改爲$ _GET,它將起作用。

在此先感謝。

+0

302如果請求的URL使用重定向邀請進行響應,則會發生302。我的猜測是,當您嘗試進行身份驗證時發生... –

回答

0

嘿,這是你應該做的。

給你的表單,你需要發送一個ID前值:

<form id ="myfrom" > 

現在,在你的Ajax調用執行下列操作,而不是

data: dataString, 

,做

data : $('#myform').serialize() , 

讓我知道如果修復它。 而在PHP的一部分我sugest,首先你做

print_r($_POST); 

只是讓你看到數據是如何傳遞。

+0

@ Marwan - 我還需要傳遞其他參數,因此我無法使用序列化。我想這個問題是OAuth身份驗證的原因。 – nickalchemist