曹永思

导航

Json跨域请求数-Jquery Ajax请求

同步请求,async(是否异步)
//同步请求,等待并接收返回的结果
var result = $.ajax({ type: "GET", url: address, async: false }).responseText;

//同步验证 验证码是否正确 
var msg = $.ajax({ type: "POST", url: "/IsVerifyCode.html?", data: "verifyCode=1978", async: false }).responseText;
if (msg == "false") {
   $.jBox.tip("验证码有误");
   $("input[name='_member.VerifyCode']").focus();
   return false;
}
var regMobile = /^(13|14|15|18)\d{9}$/;
if (!regMobile.test(msg)) {
   $.jBox.tip("请输入您的手机请输入有效的手机号码号码");
   return false;
}

数据地址:http://cy.com/Json_cross_domain/Handler1.ashx 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string jsoncall = context.Request["jsoncallback"];
        Users u = new Users();
        u.Name = "招运堂";
        u.Phone = "13457803838";
        u.Email = "zhaoyuntang@126.com";
        //JavaScriptConvert.SerializeObject(s_result);
        string json = JsonConvert.SerializeObject(u);
        context.Response.Write(jsoncall + "(" + json + ")");
    }
    public class Users
    {
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
View Code

 跨域请求,返回Json结果

    <script src="http://cy.com/Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function getdata() {
            //数据地址
            var url = "http://cy.com/Json_cross_domain/Handler1.ashx?u=1&jsoncallback=?&id=" + Math.random();
            $.ajax({
                type: 'get',
                url: url,
                dataType: "json",
                success: function (data) {
                    var s = data.Name;
                    alert(s);
                },
                complete: function (XMLHttpRequest, textStatus) {
                    kaiguan = true;
                },
                error: function () {
                    alert("抱歉,服务器遇到问题!");
                }
            });
        }
View Code
   $.ajax({
                    //提交数据的类型 POST GET
                    type: "GET",
                    //提交的网址
                    url: "/AppQuestion/SetAnswerAppQuestion/",
                    //提交的数据
                    data: { qid: qid, c: c },
                    //返回数据的格式
                    datatype: "html", //"xml", "html", "script", "json", "jsonp", "text".
                    //在请求之前调用的函数
                    beforeSend: function () { $.jBox.tip("正在发送请求...", 'loading'); },
                    //成功返回之后调用的函数             
                    success: function (data) {
                        var t = data.indexOf('ok');
                        if (t >= 0) {
                            $.jBox.tip("提交成功", 'success');
                            $("#idTrshowWrite").show();
                            $("#idTrtoSubmit").hide();
                            $("#textarea_contents").value = "";
                        }
                        else
                            $.jBox.tip("操作失败【" + msg + "】", 'error');


                    },
                    //调用执行后调用的函数
                    complete: function (XMLHttpRequest, textStatus) {
                        alert(XMLHttpRequest.responseText);
                        alert(textStatus);
                        //HideLoading();
                    },
                    //调用出错执行的函数
                    error: function () {
                        //请求出错处理
                    }
                });
View Code

 

点击下载Newtonsoft.Json.dll

posted on 2014-12-03 15:36  曹永思  阅读(243)  评论(0编辑  收藏  举报