0

我的大学成绩卡(结果)很难理解,而且很难计算学生的实际百分比。所以,我想为学生提供一项服务,学生只需要在那里输入注册号。我会自己计算结果。

首先,我尝试向 http://hurl.it 中的成绩卡页面发送帖子请求 是烫发链接http://hurl.it/hurls/c61f1d38b6543965151a1c8a8d6f641b8921da49/986ecba51b61022fa9fa162be612d7c928051989

但是从我的网站使用 jquery 发送相同的请求时出现错误:我正在使用以下代码发送请求。

            $.ajax({
                type: "POST",
                url: "http://stusupport12.ignou.ac.in/Result.asp",
                data: "submit=Submit&eno=092853268&hidden%5Fsubmit=OK&Program=BCA",
                success: function (d) {
                    $("#resultDiv").html(d);
                },
                error: function (a, b, c) { alert(a + b + c); }
            });

请朋友们帮帮我。

更新 2 - 我通过在我的服务器上处理请求来找到我的解决方案。我创建了一个通用处理程序 (.ashx) 来处理服务器上的请求并将处理后的请求发送回客户端。我可以用 Jquery 来调用它。

这是代码

<%@ WebHandler Language="C#" Class="IGNOU" %>

using System;
using System.Web;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net;
using System.IO;

public class IGNOU : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        context.Response.Write(Func(context.Request.QueryString["eno"]));
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
    public string Func(string eno)
    {
        string str = string.Empty;
        WebRequest request = WebRequest.Create("http://stusupport12.ignou.ac.in/Result.asp");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Create POST data and convert it to a byte array.
        string postData = "submit=Submit&Program=BCA&hidden_submit=OK&eno=" + eno;
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

        return responseFromServer;
    }
}

更新 1添加了网页链接 https://www.bobdn.com/IGNOU_BCA_Result.aspx

4

3 回答 3

1

此错误是由于跨域策略,响应返回正确的数据但是浏览器本身阻止响应,因为它不是同一来源。

正如您在 twitter 上看到的,https://twitter.com/crossdomain.xml这是跨域策略,您需要crossdomain.xml在根目录中放置一个文件,stusupport12.ignou.ac.in其内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<cross-domain-policy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFile.xsd">
  <allow-access-from domain="www.bobdn.com" />
</cross-domain-policy>

我的 asp.net 样机:

byte[] buffer = Encoding.UTF8.GetBytes("submit=Submit&eno=092853268&hidden%5Fsubmit=OK&Program=BCA");

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://stusupport12.ignou.ac.in/Result.asp");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;
req.CookieContainer = new CookieContainer(); // enable cookies 

Stream reqst = req.GetRequestStream(); // add form data to request stream
reqst.Write(buffer, 0, buffer.Length);
reqst.Flush();
reqst.Close(); 
于 2012-05-10T16:22:18.277 回答
0

我的猜测是错误不在函数声明function (a, b, c)中,而是在警报中。我同意这a, b, c是可怕的变量名称 - 特别是 b/c 并不明显这a是一个 XMLHttpRequest 对象,而bc是字符串。
如果您意识到这一点,您将很容易看到错误 - 无法将 XMLHttpRequest 对象与字符串连接。

在此处查看有关错误功能的文档:http: //api.jquery.com/jQuery.ajax/

于 2012-05-10T15:48:40.150 回答
0

这是重点:

您引用的站点(hurl.it)实际上使用服务器端脚本,使其服务器充当代理并为您检索结果。我绝对肯定,由于同源策略,您无法在客户端执行此类请求。

想象一下,您可以自由地执行这样的请求,您将能够使用大量未过滤的呼叫加载其他人的服务器,从而分配来自数千个不同 IP 的呼叫,并且无法引用您的服务。这就是为什么 SOP 要求您在两者之间放置自己的服务器以监控和引导这些请求。

另一种解决方案是远程服务器设置 API 服务以接收客户端调用,从而返回 JSON 响应,这将正常工作。几乎所有 API 服务都需要一个额外的参数,这将是与您的域相关联的个人开发人员密钥,因此他们可以在任何情况下知道这些请求来自哪里

于 2012-05-10T17:00:54.500 回答