我有一個問題,發佈數據到WebaPi控制器在HTML頁面中使用Jquery ajax ..當我通過提琴手測試它的意圖和工作正常,但是當我嘗試使用Ajax它只給出了錯誤..無法發佈數據到WebApi使用jQuery的HTML5
這裏是我的示例代碼..
var bookData = {
"CurrentCompany": "BizSight Sample Product Company",
"CompanyName": CompanyName,
"LegalName": "Biztech",
"Address1": Address1,
"Address2": Address2,
"City": City,
"State": State,
"Phone": Phone,
"ZipCode": ZipCode,
"Fax": Fax,
"Email": Email,
"Website": Website,
"CountryID": 1,
"federalTaxID": 1,
"CurrentUser": CurrentUser,
"BusinessType": BusinessType,
"CurrentFiscalYear": Jsondate
};
$.ajax({
type: "POST",
cache: false,
url: "http://77b2130d5c3446eea4e4210c51529230.cloudapp.net:8080/Create/",
data: JSON.stringify(bookData),
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (data, status, jqXHR) {
var result = JSON.stringify(data);
$("#loading").show();
alert("success..." + result);
},
error: function (xhr) {
$("#loading").show();
alert("Test3:" + xhr.statusText);
}
});
,當我從HTML發佈的數據,並檢查提琴手它給消息像下面的圖片
我錯過了這段代碼中的任何內容..?提琴手它自它的工作,但frmo html頁面無法正常工作..可以請你幫我如何解決和實現這一
我的API類`
public class CreateController : ApiController
{
public static string CompanyName = " ";
public static string con;
public static string sqlConnection;
public static string DatabaseName;
string TrustedConnection = "Yes";
string DBServer = " ";
string FilePath = null;
string testFirstline = " ";
//clsLog objLog = new clsLog();
bool isSuccess = false;
public string Post(DatabaseCls cls)
{
string ReturnMessage = "Success";
// ReturnMessage = "Hello " + id + "_" + parameter1 + "_" + parameter2 + "_" + parameter3 + "_" + parameter4 + "_" + parameter5 + "_" + parameter6 + "_" + parameter7 + "_" + parameter8 + "_" + parameter9 + "_" + parameter10 + "_" + parameter11 + "_" + parameter12 + "_" + parameter13 + "_" + parameter14 + "_" + parameter15;
return ReturnMessage;
}}
感謝 維克多.A
你確定你需要這樣做:'數據:JSON.stringify(bookData),' –
你能告訴你所得到的錯誤,的WebAPI方法和你試圖序列化的類?如果在從ajax調用該方法時發現該方法,但發現錯誤,那麼很可能您的對象不會序列化爲您指定的類型,或者您的webAPI尚未設置爲允許CORS請求。 –
@timothy,我需要將json數據傳遞給我的api ..有沒有其他的..? –