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

前端之家收集整理的这篇文章主要介绍了Jquery – 使用POST请求,这是不允许的错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想让 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服务方法标有ScriptMethodAttribute,指定UseHttpGet = true.尝试删除此参数,或将其设置为false.这是阻止POST工作的原因.

猜你在找的jQuery相关文章