Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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
Jquery-使用POST请求,这是不允许的错误_Jquery - Fatal编程技术网

Jquery-使用POST请求,这是不允许的错误

Jquery-使用POST请求,这是不允许的错误,jquery,Jquery,我正在尝试让jquery与web服务通信 function Test(item) { $.ajax({ type: 'POST', url: 'WebService.asmx/Test', data: '{ "Item" : "' + item + '" }', contentType: 'application/json; charset=utf-8', dataType: 'json',

我正在尝试让jquery与web服务通信

  function Test(item) {
    $.ajax({
        type: 'POST',
        url: 'WebService.asmx/Test',
        data: '{ "Item" : "' + item + '" }',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            alert("oi");
        },
        error: function (msg) {
            alert('Get Details Failure: ' + msg);
        }
    });
};

using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {}

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)] 
    public string Test(string Item)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(Item);
        return strJSON;
    }
我得到以下信息:

{"Message":"An attempt was made to call the method \u0027Test\u0027 using a POST request, which is not allowed.","StackTrace":"   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

您的web服务方法用指定
UseHttpGet=true
ScriptMethodAttribute
标记。请尝试删除此参数,或将其设置为
false
。这就是阻止POST工作的原因。

那么,使用GET请求(或者将webservice方法更改为接受POST)

请注意,
data
参数不是字符串(特别是,它不是JSON)。您应该传递一个JavaScript对象

function Test(item) {
    $.ajax({
        type: 'GET',
        url: 'WebService.asmx/Test',
        data: {Item: item }, /* note change here, data is NOT a string! */
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            alert("oi");
        },
        error: function (msg) {
            alert('Get Details Failure: ' + msg);
        }
    });
};