[英]What is the right way of sending a var on AJAX
我有一個調用Webmethod的javascript函數。 我嘗試將常規字符串發送到web方法,並且可以正常工作。 在var empid = $('#'+ txtId).val()我得到了正確的文本框值。 通過ajax發送Empid的正確方法是什么? 我嘗試了幾件事,但他們沒有用。 任何幫助,將不勝感激。 謝謝
.js
function toggle(txtId, lblname, txtcode) {
var empid = $('#' + txtId ).val();
$.ajax({
type: "POST",
url: "SearchEmpId.asmx/GetEmployeeName",
data: '{ id: empid }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('#' + lblname).html(data.d);
}
});
}
.asmx.vb(網絡方法)
Public Function GetEmployeeName(ByVal id As String) As String
Return "It works"
End Function
這是刪除contentType時的屏幕截圖
使用dataType:“ json”獲取json數據
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
dataType:'json', // add json datatype to get json
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
閱讀文檔http://api.jquery.com/jQuery.ajax/
同樣在PHP中
<?php
$userAnswer = $_POST['name'];
$sql="SELECT * FROM <tablname> where color='".$userAnswer."'" ;
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
// for first row only and suppose table having data
echo json_encode($row); // pass array in json_encode
?>
您可以如下使用:
var empid=$('#txtEmpId').val();
在數據中傳遞Empid作為
data: { id: empid },
在這里查看小提琴: http : //jsfiddle.net/gN6CT/103/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.