Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何解决Ajax上的无效JSON原语错误?_Javascript_Jquery_Json_Ajax_Webmethod - Fatal编程技术网

Javascript 如何解决Ajax上的无效JSON原语错误?

Javascript 如何解决Ajax上的无效JSON原语错误?,javascript,jquery,json,ajax,webmethod,Javascript,Jquery,Json,Ajax,Webmethod,我正在尝试在Asp.net上使用webmethod执行ajax 我正在使用JQuery-3.1.1 我想将该值传递给服务器并获取该值。我不知道我做错了什么 Asp.net代码 <div class="row"> <asp:Button ID="btnSave" runat="server" CssClass="btn btn-info" Text="save" />

我正在尝试在Asp.net上使用webmethod执行ajax

我正在使用JQuery-3.1.1

我想将该值传递给服务器并获取该值。我不知道我做错了什么

Asp.net代码

<div class="row">
   <asp:Button ID="btnSave" runat="server" CssClass="btn btn-info"  Text="save" />
</div>
$(document).ready(function () {
    $('#btnSave').click(function () {
        var name= 'xxx';

        $.ajax({
            type: "POST",
            url: "AjaxWebMethod.aspx/getFileExistOrNot",
            data: '{fileName:'+name+'}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert("failed ="+response.d);
            },
            error: function (response) {
                alert("error ="+response.d);  //This alert is executing
            }
        });

        function OnSuccess(response) {
            alert("Result ="+response.d.mydata);
        }
        return false;
    });
});
public partial class AjaxWebMethod : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string getFileExistOrNot(string fileName)
    {
        string mydata="ajaxworking";
        return mydata;
    }
}
C#代码

<div class="row">
   <asp:Button ID="btnSave" runat="server" CssClass="btn btn-info"  Text="save" />
</div>
$(document).ready(function () {
    $('#btnSave').click(function () {
        var name= 'xxx';

        $.ajax({
            type: "POST",
            url: "AjaxWebMethod.aspx/getFileExistOrNot",
            data: '{fileName:'+name+'}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert("failed ="+response.d);
            },
            error: function (response) {
                alert("error ="+response.d);  //This alert is executing
            }
        });

        function OnSuccess(response) {
            alert("Result ="+response.d.mydata);
        }
        return false;
    });
});
public partial class AjaxWebMethod : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string getFileExistOrNot(string fileName)
    {
        string mydata="ajaxworking";
        return mydata;
    }
}
我真的不知道我在哪里犯了错

控制台上的错误消息

<div class="row">
   <asp:Button ID="btnSave" runat="server" CssClass="btn btn-info"  Text="save" />
</div>
$(document).ready(function () {
    $('#btnSave').click(function () {
        var name= 'xxx';

        $.ajax({
            type: "POST",
            url: "AjaxWebMethod.aspx/getFileExistOrNot",
            data: '{fileName:'+name+'}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert("failed ="+response.d);
            },
            error: function (response) {
                alert("error ="+response.d);  //This alert is executing
            }
        });

        function OnSuccess(response) {
            alert("Result ="+response.d.mydata);
        }
        return false;
    });
});
public partial class AjaxWebMethod : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string getFileExistOrNot(string fileName)
    {
        string mydata="ajaxworking";
        return mydata;
    }
}
{消息:“无效的JSON原语:xxx.”,…}

ExceptionType:“System.ArgumentException”

消息:“无效的JSON原语:xxx。”


你能给出合理的解决方案吗。这将有助于我了解更多信息,谢谢

您正在进行的jQuery调用被设置为在响应中使用JSON格式。服务器端代码似乎只是返回一个未编码为JSON的字符串。您需要更改服务器的输出或更改ajax调用的预期内容。

您正在进行的jQuery调用被设置为在响应中使用JSON格式。服务器端代码似乎只是返回一个未编码为JSON的字符串。您需要更改服务器的输出或更改ajax调用的预期内容。

您的postData是无效的JSON,字符串需要在其周围加引号:

'{fileName:'+name+'}'
应该是:

 '{fileName:"'+name+'"}'

您的postData是无效的JSON,字符串需要加引号:

'{fileName:'+name+'}'
应该是:

 '{fileName:"'+name+'"}'

string mydata=“ajaxworking”无效JSON
string mydata=“ajaxworking”不是有效的JSONTANKS。在你的更新答案上,你能告诉我jquery中get和post方法的区别吗?。谢谢你again@mohamedfaiz,如果你有一个新问题,你应该把它作为一个新问题发布,而不是在评论中提问。只有在回答了原始问题的情况下,你才接受他们的回答。Get不允许你发布数据,你只能发送url上不太安全且长度有限的内容。如果您需要将数据发布到服务器,最好使用post,post还允许大量数据:@trincot我准备接受他的答案,但请让我再等待五分钟接受答案。谢谢。在你的更新答案上,你能告诉我jquery中get和post方法的区别吗?。谢谢你again@mohamedfaiz,如果你有一个新问题,你应该把它作为一个新问题发布,而不是在评论中提问。只有在回答了原始问题的情况下,你才接受他们的回答。Get不允许你发布数据,你只能发送url上不太安全且长度有限的内容。如果您需要将数据发布到服务器,最好使用post,post还允许大量数据:@trincot我准备好接受他的答案,但请让我再等五分钟接受答案。