Ajax调用不返回正确的结果为字符串 [英] Ajax call does not return proper result as string
本文介绍了Ajax调用不返回正确的结果为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Ajax调用。我没有得到的字符串所需的响应。谁能帮我在这方面。先谢谢了。
函数功能1(){
警报(在Ajax调用);
$阿贾克斯({
键入:POST,
网址:abc.aspx / MyFunction1
数据类型:JSON
的contentType:应用/ JSON的;字符集= UTF-8,
成功:函数(RES){
$('#regularticker')HTML(res.d);
//$('#futureticker').html(res.d);
变种D =新的日期(); // 目前
$('#注册')的HTML(更新为+ d.getHours()+:+ d.getMinutes()+:+ d.getSeconds());。
//警报(res.d.toString());
警报(RES);
警报(res.d);
},
错误:功能(RES){
} });
}
名为
我的后端功能
[的WebMethod]
公共静态字符串MyFunction1()
{
尝试
{
如果属实)
{
返回测试;
}
}
赶上(异常前)
{
返回ex.Message;
}
}
解决方案
您code工作正常在我machine.May是你忘了通过文档加载jQuery中调用它。我的code
<脚本SRC =https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js>< / SCRIPT&GT ;
<脚本类型=文/ JavaScript的>
$(函数(){
功能1();
});
功能功能1(){
警报(在Ajax调用);
变种PARAMS = {};
params.parameter =传递数据串;
$阿贾克斯({
键入:POST,
网址:abc.aspx / MyFunction1
数据类型:JSON
的contentType:应用/ JSON的;字符集= UTF-8,
数据:JSON.stringify(PARAMS),//无数据注释本节
成功:函数(RES){
// $('#regularticker')HTML(res.d);
// //$('#futureticker').html(res.d);
//变种D =新的日期(); // 目前
// $('#注册')的HTML(更新为+ d.getHours()+:+ d.getMinutes()+:+ d.getSeconds());。
// //警报(res.d.toString());
//警报(RES);
警报(res.d);
},
错误:功能(RES){
} });
}
< / SCRIPT>
和隐藏文件我的code
[的WebMethod]
公共静态字符串MyFunction1(字符串参数)
{
//尝试
// {
// 如果属实)
// {
//返回测试;
//}
//}
//赶上(异常前)
// {
//返回ex.Message;
//}
返回参数;
}
My ajax call. I am not getting the desired response as string. Can anyone help me in this regard. Thanks in advance.
function Function1() {
alert("In Ajax Call");
$.ajax({
type: "POST",
url: "abc.aspx/MyFunction1",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (res) {
$('#regularticker').html(res.d);
//$('#futureticker').html(res.d);
var d = new Date(); // for now
$('#updateTime').html("Update at " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
//alert(res.d.toString());
alert(res);
alert(res.d);
},
error: function (res) {
}
});
}
My Backend Function Called
[WebMethod]
public static string MyFunction1()
{
try
{
if (true)
{
return "test";
}
}
catch(Exception ex)
{
return ex.Message;
}
}
解决方案
Your code is working fine in my machine.May be you forgot to call it by document load in Jquery. My code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
Function1();
});
function Function1() {
alert("In Ajax Call");
var params = {};
params.parameter = "passing string data";
$.ajax({
type: "POST",
url: "abc.aspx/MyFunction1",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(params),//No data comment this section
success: function (res) {
// $('#regularticker').html(res.d);
// //$('#futureticker').html(res.d);
// var d = new Date(); // for now
// $('#updateTime').html("Update at " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
// //alert(res.d.toString());
// alert(res);
alert(res.d);
},
error: function (res) {
}
});
}
</script>
and my code behind file
[WebMethod]
public static string MyFunction1(string parameter)
{
//try
//{
// if (true)
// {
// return "test";
// }
//}
//catch (Exception ex)
//{
// return ex.Message;
//}
return parameter;
}
这篇关于Ajax调用不返回正确的结果为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文