Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to create a HTTP request from JavaScript to PHP and send a big string to the server as a file. I can make this in Java, but I need it in JavaScript.

What I have in Java:

private static String serviceUrl = "http://www.site.com/page.php";
private static String boundary = " " + System.currentTimeMillis();
private static String line;

public static String execute(String params) throws IOException {
    URL url = new URL(serviceUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    // Write request

    // Request Heading
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Accept", "*/*");
    conn.setRequestProperty("Content-Type",
            "multipart/form-data; boundary=" + boundary);

    // Request Body
    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
    out.write("--" + boundary + "\n");
    out.write("Content-Disposition: form-data; name=\"xml\"; filename=\"small\"\n");
    out.write("Content-Type: application/octet-stream\n");
    out.write("\n");

    out.write(params);

    // Finishing request body
    out.write("\n");
    out.write("--" + boundary + "--\n");
    out.flush();
    out.close();

    // Read response
    BufferedReader in = new BufferedReader(new InputStreamReader(
            conn.getInputStream()));
    String response = "";
    while ((line = in.readLine()) != null) {
        System.out.println(line);
        response = response + line;
    }
    in.close();
    return response;
}   

How can I turn this in JavaScript? Is it possible?

EDIT:

Hi, the purpose of this question was not to have the code rewritten.

The thing is I could solve the problem in Java, but I needed to solve it in javascript and had no idea where to start, I was actually looking for pointers, as the ones that were given here.

I apologize for not have written a clear question and thank the ones who understood it anyways.

share|improve this question

closed as off-topic by bfavaretto, Eran, Tom Studee, Sven, Jeremy J Starcher Jul 16 '13 at 21:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – bfavaretto, Eran, Tom Studee, Sven, Jeremy J Starcher
If this question can be reworded to fit the rules in the help center, please edit the question.

    
You have to use ajax (XMLHttpRequest). –  bfavaretto Jul 16 '13 at 20:11
1  
javascript and java are 2 completely different languages. –  Aris Jul 16 '13 at 20:11
3  
Stack Overflow is best suited to get help on your current progress. You're essentially asking us to rewrite all of your code into javacript, which is unreasonable and the cause of your downvotes. Try looking up some javascript tutorials and come back with specific questions for best results. –  leigero Jul 16 '13 at 20:12
    
@Aris Why does that matter? The Java code is making an HTTP request...the OP wants to know how to do it in JavaScript. If the Java code was doing something ridiculous, maybe pointing out Java vs. JavaScript would be beneficial... –  Ian Jul 16 '13 at 20:26

2 Answers 2

Anything is possible. In javascript, you want to look at the XMLHTTPRequest Object (XHR), which despite it's name need not be used to request XML.

The semantics of how you send the message body will be different, but since you can figure out how to build the Java version, I'm guessing once you have the link to the JS docs you'll figure it out there too (or at least ask a question about XHR specifically)

share|improve this answer

http://www.w3schools.com/ajax/ is a very good tutorial, which I used. I recommend reading through that, it should give you everything you need. (The XMLHttpRequest section is the main bit you need.)

share|improve this answer
1  
Let me just suggest not using W3Schools...MDN has better, more complete, and accurate information: developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest –  Ian Jul 16 '13 at 20:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.