0

My University grade card (result) is very complex to understand and It is very hard to calculate actual percentage of a student. So, I want to provide students a service where student will just need to enter there enrollment number. And I will calculate result my self.

Very firstly, I tried to send a post request to grade card page in http://hurl.it Here is the perm link http://hurl.it/hurls/c61f1d38b6543965151a1c8a8d6f641b8921da49/986ecba51b61022fa9fa162be612d7c928051989

But I am getting error when sending same request using jquery from my website: I am using following code to send request.

            $.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); }
            });

Please help me friends.

Update 2 - I find my Solution by processing the request on my server. I'd created a Generic Handler (.ashx) that handles the request on server and send the processed request back to client. I can call it by Jquery.

Here is the code

<%@ 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;
    }
}

Update 1 Added link to web page https://www.bobdn.com/IGNOU_BCA_Result.aspx

shashwat
  • 7,851
  • 9
  • 57
  • 90
  • 3
    you cannot perform cross domain ajax unless you get a JSON/JSONP object as response – Carlo Moretti May 10 '12 at 15:38
  • @ocanal in `error: function (a, b, c)`. It is just an error. No detail provided. – shashwat May 10 '12 at 15:40
  • 1
    Why would you use `a`, `b`, `c` as variable names. That sucks. – PeeHaa May 10 '12 at 15:41
  • @Onheiron hurl.it can do it. Why I can't. check this link http://hurl.it/hurls/c61f1d38b6543965151a1c8a8d6f641b8921da49/986ecba51b61022fa9fa162be612d7c928051989 – shashwat May 10 '12 at 15:42
  • I also tried using `error: function OnError(request, status, error)`. I am getting same error with this too @RepWhoringPeeHaa – shashwat May 10 '12 at 15:46
  • Yes I am, please don't laugh on my marks :-) @zod – shashwat May 10 '12 at 15:47
  • @newtostackoverflow, hurl.it can do it because it doesn't make request in javascript side, I did not look into, but it should make request in server-side ( like [curl in php](http://php.net/manual/en/book.curl.php) ). I'm not sure that is the problem you have, actually you should get an error , http://jsfiddle.net/mYEtu/ – Okan Kocyigit May 10 '12 at 16:06

3 Answers3

1

This error is due to crossdomain policy, the response is coming back with the correct data however the browser itself is blocking the response because it is not same origin.

As you can see on twitter https://twitter.com/crossdomain.xml this is the cross domain policy, you will need to place a crossdomain.xml file in the root of stusupport12.ignou.ac.in with the contents like:

<?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>

My asp.net mockup:

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(); 
Bloafer
  • 1,324
  • 7
  • 12
  • I do not have access to the server of University. How can I do this..?? Is there any alternate..?? can I process this request on server side using asp.net with C#..???? please help – shashwat May 10 '12 at 16:28
  • The way to bypass crossdomain policy in PHP is to use a cURL wrapper. By making a post call server side you will bypass the restrictions. – Bloafer May 10 '12 at 16:30
  • can't I make post call from asp.net in server side..?? – shashwat May 10 '12 at 16:33
  • I am not an ASP.net developer, but from what I hove found it would be something like the amended code to send the request. – Bloafer May 11 '12 at 07:56
0

my guess is that the error is not in the function declaration function (a, b, c), but in the alert. I agree that a, b, c are horrible variable names - in particular b/c it is not obvious that a is an XMLHttpRequest object, while b and c are strings.
If you realize that, you will easily see the error - cant concat the XMLHttpRequest object with a string.

See the docs about the error function here: http://api.jquery.com/jQuery.ajax/

Stephen
  • 853
  • 3
  • 11
  • 25
  • dear while analyzing request using HttpFox (add on in firefox). I found that the error is `Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED)`. I double checked the `url`. There is no error in url. You can also see that. Same url in hurl.it working – shashwat May 10 '12 at 15:52
  • I think this question has your answer there: http://stackoverflow.com/questions/4038299/why-does-this-javascript-work-on-safari-but-not-firefox as Onheiron said - cannot cross domain post ajax without JSONP – Stephen May 10 '12 at 15:54
  • may be the error is `text/html (NS_ERROR_DOM_BAD_URI)` I see this in httpfox somewhere – shashwat May 10 '12 at 15:57
  • different error msg for the same basic problem. see: http://stackoverflow.com/questions/1105055/ajax-and-ns-error-dom-bad-uri-error – Stephen May 10 '12 at 16:04
  • see here for an example using jsonp request: http://stackoverflow.com/questions/1002367/jquery-ajax-jsonp-ignores-a-timeout-and-doesnt-fire-the-error-event – Stephen May 10 '12 at 16:06
  • also keep in mind that the error function wont be called for jsonp requests according to jquery docs in the link in my original answer. Note: This handler is not called for cross-domain script and JSONP requests. – Stephen May 10 '12 at 16:08
0

Here is the point:

The site you refer to (hurl.it) actually uses server side scripta making its server act as a proxy and retriving the results for you. I am absolutely positive you cannot perform such request client side because of the Same Origin Policy.

Imagine you can do such a request freely, you would be able to load someone else's server with thons of unfiltered calls, thus allot of calls coming from thousands of different IPs and not referable to your service. This is why SOP asks you to put your own server in between in order to monitor and direct those requests.

Another solution would be the remote server to setup an API service to receive client side calls, thus returning JSON responses which will work fine. Preatty much all API services althought require an extra parameter which would be your personal developer key associated with your domain so they can in any case know where those requests come from

Carlo Moretti
  • 2,213
  • 2
  • 27
  • 41