发帖JSON到ASP.NET Web服务(ASMX)只获得尽可能选项,而不是完整的POST [英] Posting JSON to ASP.NET web service (ASMX) only getting as far as OPTION and not full POST

查看:89
本文介绍了发帖JSON到ASP.NET Web服务(ASMX)只获得尽可能选项,而不是完整的POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在一个jQuery脚本帖子JSON到我的asp.net web服务。如果脚本是在同一个域是正确处理,但尽管我已经注意到的是,在我的网络日志中,当我尝试从本地文件或在备用站点脚本不会出现发生在眼球!..试图到​​达服务器但方法选项,而不是POST。

I have been working on a JQuery script that posts JSON to my asp.net web service. If the script is on the same domain it processes correctly but when I try to run the script from a local file or on an alternative domain not appears to happen to the eye!.. Though what I have noticed is that within my web logs the attempt is reaching the server but with the method OPTIONS rather than POST.

我试图在IIS中设置下面的标题,直接的web.config和我的Global.asax文件来对事件的Application_BeginRequest,但没有成功,在迄今!

I have tried to set the following headers within IIS, web.config directly and within my global.asax file to on the application_beginRequest event but no success thus far!

访问控制允许来源*

访问控制允许的方法GET,POST。

Access-Control-Allow-Methods GET, POST.

我的Global.asax文件包含此:

My global.asax file contain this:

protected void Application_BeginRequest(object sender, EventArgs e)
{

    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();

    EnableCrossDmainAjaxCall();
}

private void EnableCrossDmainAjaxCall()
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
                      "GET, POST, OPTIONS");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
                      "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
                      "1728000");
        HttpContext.Current.Response.End();
    }
}

我的客户端脚本如下:

My client side script is as follows:

$.ajax({
            type: "POST",
            crossDomain: true,
            url: "http://myservice.mydomain.com/Data.asmx/Import",
            data: "{\"AppSession\":" + JSON.stringify(session) + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                // Insert the returned HTML into the <div>.
                $('#content').html(data.d);
            },
            error: function (j, t, e) {

                $('#content').html(e);
            }
        });

我的web服务code是如下...

My web service code is as follows...

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;

/// <summary>
/// Summary description for DataMgt
/// </summary>
[WebService(Namespace = "DataManagement")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]

public class DataMgt : System.Web.Services.WebService {

    public DataMgt () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 

    }
    public class GeoData
    {
        public string GEOLongitude { get; set; }
        public string GEOLatitude { get; set; }
        public string GEOAccuracy { get; set; }
        public string GEOStatusID { get; set; }
    }

    public class AppSession
    {
        public GeoData GEOData { get; set; }
        public string UserID { get; set; }
        public string CountryID { get; set; }
    }

    [WebMethod]

    public string ImportData(AppSession AppSession)
    {
        string strStatusCode;
        try
        {
            //Do stuff here with requested data

            strStatusCode = "SUCCESS";
        }
        catch (Exception ex)
        {
            strStatusCode = "FAILURE";
        }

        return strStatusCode;

    } 
}

正如我刚才所说,当客户端脚本是在同一个域运行此运行精美绝伦。

As I said earlier, this runs absolutely fine when the client side script is run on the same domain.

承载我的服务网站是IIS 7和ASP.NET 4。

The website that hosts my service is on IIS 7 and ASP.NET 4.

有没有人expereinc​​ed此使用类似的设置给我或任何人都可以提出如何解决这个时候?

Has anyone expereinced this when using a similar setup to me or can anyone suggest how to resolve this?

在事先非常感谢,

丽特

推荐答案

如果您在不同势领域工作,你将无法POST请求到服务器。它的限制,你必须使用JSONP

If you working on diffrent domain you will not be able to POST request to your server. Its restricted, you have to use JSONP

JSONP:
http://api.jquery.com/jQuery.getJSON/

一个教程:
<一href=\"http://abstractform.word$p$pss.com/2009/10/12/accessing-remote-asp-net-web-services-using-jsonp/\" rel=\"nofollow\">http://abstractform.word$p$pss.com/2009/10/12/accessing-remote-asp-net-web-services-using-jsonp/

这篇关于发帖JSON到ASP.NET Web服务(ASMX)只获得尽可能选项,而不是完整的POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆